Alasse
Alasse

Reputation: 361

Executable jar file error

Hey there i am developing java application. I completed coding part but i am recieving exception in thread main java.lang.noclassdeffounderror when i ran it. I have searched this through the internet, and could not find suitable answer.

Let me explain what i have done before exporting java program to the executable jar file from eclipse:

When i ran the jar file from cmd by typing java -jar ssc.jar It gives me this error:

exception in thread main java.lang.noclassdeffounderror

and some other lines with tag.

What should i do to fix this?

Some documentations say that i need add classpath to program, or edit manifest file etc. I could not figure out how.

I would appreciate your help. Thanks

(Edit)SSC.java class under the demo package. It works when i run from the eclipse:

package demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import com.systemsunion.ssc.client.*;

public class SSC {

    static String HOST="localhost";
    static int Port=8080;

    public static void main(String[] args) {
        try
        {
            SecurityProvider secman= new SecurityProvider(HOST,true);           
            String voucher= secman.Authenticate("PKP","").toString();
            String sInputPayload="";
            String path="C:/SSC temp/temp.txt";
            BufferedReader reader= new BufferedReader(new FileReader(path));
            BufferedWriter writer= new BufferedWriter(new FileWriter(new File("C:/SSC temp/temp-result.txt")));
            String line="";
            while((line = reader.readLine()) != null)
            {
                sInputPayload = sInputPayload + line;
            }
            try
            {
                SoapComponent ssc= new SoapComponent(HOST, Port);
                ssc.authenticate(voucher);
                String result= ssc.execute("Journal", "Import", sInputPayload);
                writer.write(result);
                writer.newLine();
                writer.close();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Error when i ran the jar file from cmd:

Exception in thread "main" java.lang.NoClassDefFoundError: com/systemsunion/ssc/

    client/SoapComponent
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
            at java.lang.Class.getMethod0(Unknown Source)
            at java.lang.Class.getMethod(Unknown Source)
            at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: com.systemsunion.ssc.client.SoapCom
    ponent
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 6 more

Upvotes: 2

Views: 10850

Answers (3)

ADTC
ADTC

Reputation: 10121

The problem is with "Copy required libraries into a sub-folder next to the generated JAR". When you do this, the libraries are copied to a sub-folder in the same folder as the created jar file. When you attempt to run the jar file, you must add all the jars this sub-folder to the classpath (I'm not sure if wildcards work - I think they will with Java 7):

java -cp sub\jar1.jar;sub\jar2.jar -jar ssc.jar

To avoid this issue (having to add all the jar files is cumbersome), you can use either of these:

  • Extract required libraries into generated JAR
  • Package required libraries into generated JAR

The advantage is: your application JAR will now travel with all dependencies built-in so it will always work. The disadvantage is: a JAR bloated with the dependencies inside (potentially resulting in a very large JAR file, out of which maybe only a small part is your actual application).

Edit: You may also need to add a reference to your project in the command line above. See comments on this answer for more details.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109613

Use a META-INF/MANIFEST.MF with a Class-Path: lib/... .jar.

It might be a moment to consider using a maven plugin. Maven provides a build infrastructure for miscellaneous things. It also heavily uses conventions; other default paths unfortunately, src/main/java, but it's worth it. So with maven first start a new project. But then library version dependencies, packaging with libraries and such come free.

Upvotes: 1

Ingo
Ingo

Reputation: 36339

You need to tell what your main class is before you run the jar. For easy cases you can do it like this:

jar  -uvfe  ssc.jar  your.main.class.Name

Note that you must specify the full name of your main class (the one that contains the main method). This will add a standard manifest to your jar and you can henceforth run it.

Another alternative would be to run it thus:

java -cp ssc.jar your.main.class.Name 

Edit:

The problem seems to be that you a) have no clue what the name of your main class is b) your development environment is lacking.

For a)

Look up the definition of your main class, this is the class that has:

public static void main(String[] args) { ... }

Note the class name (maybe SSC from your comments). Browse to the beginning of the file and look for the package statement. Prepend the package name to youir classname like

package org.brave.programmers

public class SSC { ... }

Here the class name would be org.brave.programmers.SSC. If there is no package, it is just SSC

For b) Make sure you have java.exe and jar.exe in your path. If you don't have jar.exe, something is wrong with your installation.

Please do a

 jar -tvf ssc.jar

to see what is in your jar file.

Upvotes: 2

Related Questions