Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How to Inlcude external Libraries in output Jar?


    relleum

    Recommended Posts

    Hello im kinda new to java development. Im trying to send a POST to my Server via HTTP. I included the apache.org httpclient libraries in my project with maven:

    Spoiler

    This is the maven pom file

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>groupId</groupId>
        <artifactId>Template_Dreambot</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.13</version>
            </dependency>
        </dependencies>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        
    </project>
    


     

    I can compile the Artifact and start it in dreambot but i get the following error message in the console:

     

    Spoiler
    
    16:05:59: [ERROR] Error occurred while trying to start script:
        java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
        at java.lang.Class.getConstructor0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.dreambot.api.script.ScriptManager.start(ScriptManager.java)
        at org.dreambot.api.script.ScriptManager.start(ScriptManager.java)
        at org.dreambot.11.lambda$null$2(11.java)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 8 more

    Error in console when i try running the script.


    This is my JavaClass:

    Spoiler
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.dreambot.api.methods.MethodProvider;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import java.nio.charset.StandardCharsets;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    @ScriptManifest(name = "XYZ TEST SCRIPT", description = "test template description", author = "relleum",
            version = 0.01, category = Category.UTILITY, image = "8nLFCVP.png")
    
    
    
    public class Template_Dreambot extends AbstractScript {
    
    
        public String send_data_to_server() throws IOException {
    
            // replace spaces with %20 for URL Encode
            String enc_name    = getManifest().name().replaceAll(" ", "%20");
            String enc_desc    = getManifest().description().replaceAll(" ", "%20");
            String enc_version = String.valueOf(getManifest().version()).replaceAll(" ", "%20");
            String custom_var  = "aioijoadsijodsoji".replaceAll(" ", "%20");
            // post to Monitor server
            String urlString = "http://localhost:42069/bots?name=" + enc_name + "&nameScript=" + enc_desc + "&version=" + enc_version + "&UUID=" + custom_var;
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(urlString);
    
            // Request parameters and other properties.
            List<BasicNameValuePair> params = new ArrayList<>(2);
            params.add(new BasicNameValuePair("param-1", "12345"));
            params.add(new BasicNameValuePair("param-2", "Hello!"));
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    
            //Execute and get the response.
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
    
            if (entity != null) {
                try (InputStream instream = entity.getContent()) {
                    MethodProvider.log(instream);
                    MethodProvider.log(urlString);
                    MethodProvider.log("Data sent to Server successfully");
                }
            }
            //debug messages
            //MethodProvider.log(urlString);
            sleep(1000);
            return "0";
        }
    
        public String recieve_data_to_server() {
    
            String localName = getLocalPlayer().getName();
            MessageDigest digest = null;
            try {
                digest = MessageDigest.getInstance("SHA-256");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            byte[] encodedHash = digest.digest(
                    localName.getBytes(StandardCharsets.UTF_8));
            //MethodProvider.log(encodedHash);
            String urlString = "https://localhost/me?UUID=" + encodedHash;
            return "0";
        }
    
        @Override
        public int onLoop() {
            try {
                send_data_to_server();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //recieve_data_to_server();
            return 0;
        }
    
    }

     

     

     

    I followed closely the Guide for Scripters. I can build and run the Script without the external Libraries (and without the code lines that use those external libs). I also tried including the Libraries in the Project Structure thing:

     

    image.png.942d521fbc5a0044d6cf9c5594364c86.png

     

    and also as extracted libraries:

    image.thumb.png.fbc4553e229bdfe55a2a5363850965b0.png

     

    Both have the same Error message. Also i dont know why IntelliJ wants me to surround so much stuff with try/catch. I previously (like 2-3years ago) used Eclipse but there it didnt behave like that.. Im working as an IT guy for 4 years now and touched some scripting here and there, but i never touched intelliJ or maven :D

    Is it even Possible to include external libraries like that? 

    Link to comment
    Share on other sites

    After editing my pom.xml the file size at least changed :D

    from: ~1.3kb

    to: ~9.2kb

    Spoiler
    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>groupId</groupId>
        <artifactId>Template_Dreambot</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.13</version>
            </dependency>
        </dependencies>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

     

     

    Link to comment
    Share on other sites

    5 hours ago, yeeter01 said:

    Made this guide for DB2 I think
     

    can also checkout my pom.xml file from a script I started and never finished on my github

    https://github.com/yeetware/dreambot-aio-aio/blob/main/pom.xml

    Thank you very much! After copying the pom.xml from you and editing it to fit my project it now builds it with dependencies :) I can now send http stuff to my server^^

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.