CodeMonkey
CodeMonkey

Reputation: 41

JAR File sometimes runs but doesn't work, or doesn't run at all

I created a GUI program that involves making many API calls in Eclipse. Works completely fine when running from Eclipse, BUT...

I exported my program as a JAR file and when I try to run it, from the terminal or from double-clicking, it throws a NoClassDefFoundError. I made sure all the packages were selected and the right Main-Class was chosen, still no luck.

I tried exporting it as a Runnable JAR file and when I double-click it, it runs and opens the GUI but doesn't work as intended. However, when I run it from the terminal, it works as intended. I have no clue why.

The error from the JAR file I get in the terminal is:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Don't really know the solution to this, as some sources online say to change things on my machine that involves admin access, which I dont have because I work on a company laptop.

For the Runnable JAR file, my GUI works as intended up until the first HttpRequest. I have to request an access token for oAuth, but my request isnt being made or something, because I'm getting a null return for which I check for.

Here is the code where that gets printed:

try {
        HttpPost request = new HttpPost(url);

        request.addHeader("Content-Type", "application/json");

        StringEntity params;
        params = new StringEntity("{\"grant_type\": \"authorization_code\","
                            + "\"client_id\": \"\","
                            + "\"client_secret\": \"\","
                            + "\"code\": \"" + code + "\"," + "\"redirect_uri\": \"\"}");
        request.setEntity(params);

        String content = execute(request, client);

        if (content != null) {
            textArea.append("Response received\n\n");
        } else {
            textArea.append("An error occurred. No response received, please restart the process.");
            return;
                    }

Just to be clear, the program works perfectly fine when running from Eclipse.

Not really sure how to fix this, if anyone has any ideas I would appreciate your help :)

Upvotes: 0

Views: 879

Answers (2)

CodeMonkey
CodeMonkey

Reputation: 41

I figured out a quick fix. After exporting the Runnable JAR, create a shortcut. Right-click the shortcut and choose Properties. Under the Shortcuts tab, look for target and change it to "C:\Program Files\Java\jdk1.8.0_192\bin\javaw.exe" -jar "C:\Users\{user}\Documents\program.jar"

So the location of your javaw.exe file, followed by -jar and then the location of the jar file.

Then hit apply and close the window. My program worked after that.

Upvotes: 0

Etruskas
Etruskas

Reputation: 195

That error means org.apache http-client library is not in the jar. Probably you have it linked in your Eclipse project, but it is not exported into the jar.

You have to create runable jar ("fat-jar") This is Eclipse guide:

  • From the menu bar's File menu, select Export.
  • Expand the Java node and select Runnable JAR file. Click Next.
  • In the Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
  • In the Export destination field, either type or click Browse to select a location for the JAR file.
  • Select library handling strategy: Package required libraries into generated JAR

    source

If that doesn't help, please provide screenshots, how you export this jar from Eclipse, and how org.apache http-client is linked.

Upvotes: 1

Related Questions