2

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:

  • I added some jar files to my library so my program could connect a 3rd party program. It runs from a custom Jre which is named SSC and got location in 3rd party program's(SunSystems) location folder.

  • I wrote the required java code to run the program.

  • Then i ran the project, which works perfectly when i run from eclipse.

  • I clicked on project's name then clicked Export. I selected Runnable jar file and Copy required libraries into sub-folder next to generated jar file. I picked correct class from launch configuration. Then clicked finish.

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
Alasse
  • 361
  • 2
  • 9
  • 17

3 Answers3

2

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.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • Hey there thanks for answer first of all. when i ran first one in cmd like this: jar -uvfe ssc.jar SSC.java "jar" is not recognized as an internal or external command, operable program or batch file When i try another one in cmd: java -cp ssc.jar SSC.java could not find or load main class SSC.java – Alasse Jul 23 '13 at 08:57
  • @Alasse remove the .java: `java -cp ssc.jar SSC`. Note that you have to put the full package name when you call the jar, i.e. if your `SSC` class is in package `com.mypackage`, the command should be `java -cp ssc.jar com.mypackage.SSC` – BackSlash Jul 23 '13 at 09:01
  • consider to use a manifest-file. this is much more user-friendly – desperateCoder Jul 23 '13 at 09:02
  • 1
    A runnable jar shouldn't require the specification of main class in the command line - otherwise, why is it _runnable_? – ADTC Jul 23 '13 at 09:21
  • @BlackSlash hey there. I tried that but still getting the same. It cannot find the main class. It is in the package named demo so i think i should do it like: java -cp ssc.jar demo.SSC I also tried workspacename.demo.ssc com.demo.ssc and other versions of those... Thanks for tip but does not seem to work :( – Alasse Jul 23 '13 at 10:02
  • @desperateCoder What should i do in manifest file after i added it to project? I could not figure it out. Would you please give me an example? Thank you – Alasse Jul 23 '13 at 10:03
  • @ADTC you have really nice point, but wish i could explain. I am orginally .net programmer. I have not used Java since my university education. – Alasse Jul 23 '13 at 10:05
  • Yikes. Anyway, is my answer (posted separately) any help? :) Btw, how did you deal with DLL files in .net projects? – ADTC Jul 23 '13 at 10:16
  • @desperateCoder The jar -uvfe actually creates a MANIFEST inside the jar. – Ingo Jul 23 '13 at 10:19
  • @Alasse Please make sure you can reach all important commands from your command line: java, javac, jar. javac & jar should be in the same directory as java, unless you have only a JRE. Please install a JDK if you need to work on the command line. – Ingo Jul 23 '13 at 10:21
  • @ADTC This is exactly the point - he does not have a runnable JAR. This thread is about how to make a runnable jar out of a non-runnable one. – Ingo Jul 23 '13 at 10:22
  • @Alasse: Ingos first hint creates the manifest insde the jar. you only have to specify the Class, which contains the main-method. – desperateCoder Jul 23 '13 at 10:24
  • 1
    Huh, @Ingo .. quote Alasse from question: _...I selected Runnable jar file..._ That option creates a runnable JAR file. So yes, he has a runnable JAR. Now he just needs to be able to run it. A runnable JAR does not require the specification of main class on command line, because the manifest inside the JAR should already specify it. – ADTC Jul 23 '13 at 10:25
  • @ADTC i had the same problem once. all i did to solve this, is export it once again. but that would be too easy, right? :/ i have no idea, why this sometimes happens – desperateCoder Jul 23 '13 at 10:26
  • 1
    @Ingo I created this jar from export>runnable jar in eclipse. The options i choosed should make this Jar runnable. But there is something wrong that i could not figure out. – Alasse Jul 23 '13 at 10:27
  • Besides these, i only have one class inside the project. And some jar files that i imported into project. My class has main method inside it. But however it cannot be found... – Alasse Jul 23 '13 at 10:29
  • @Alasse Please see my edit. Tell us the output of `jar -tvf ssc.jar` – Ingo Jul 23 '13 at 10:33
  • @Ingo hey i could not any exe file around here. And output of the command is: 'jar' is not recognized as an internal or external command, operable program or batch file. – Alasse Jul 23 '13 at 10:37
  • `jar.exe` should be in your JDK's **bin** folder. But it is not included in JRE. – ADTC Jul 23 '13 at 10:40
  • sadly no .exe. I only have SSC.class in bin folder under the "demo" package folder – Alasse Jul 23 '13 at 10:43
  • @Alasse without SDK you can't do serious work. It's like you need help with a car that doesn't have a gearbox. – Ingo Jul 23 '13 at 10:44
  • @Ingo man, why should not it have SDK in it? I also downloaded SDK and installed again to make sure i have it. Still no change. Program starts when i run from Eclipse. The thing you said would make sense if it did not run. – Alasse Jul 23 '13 at 10:46
  • @Alasse I meant JDK's bin folder, not your project's bin folder. It should be in `C:\Program Files\Java\jdk_(some version)\bin` (or it may be `C:\Program Files (x86)\...`) – ADTC Jul 23 '13 at 10:47
  • @Alasse Because you said your command line does not find jar (the command!), while it does find java, I assumed you don't have SDK. Then perhaps you have the wrong PATH – Ingo Jul 23 '13 at 10:48
  • @Alasse Please type `path` in your command window and show us the output – Ingo Jul 23 '13 at 10:49
  • C:\>path PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32 \WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\B inn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Micr osoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\T ools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9. 0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\100 \DTS\Binn\;C:\Program Files (x86)\Infor\SunSystems\;C:\Program Files (x86)\JexeP ack – Alasse Jul 23 '13 at 11:04
  • @Alasse I can't see the path name of your SDK bin directory there, just as I expected. You should add it first before we continue. – Ingo Jul 23 '13 at 11:09
  • I don't think it is because of that. I have tried the same command for another project(printing hello world), with same jre. It also gives the same path but works like a charm. This is custom jre, not default jre that comes with sdk. – Alasse Jul 23 '13 at 11:11
  • @Alasse I give up, you obviously won't understand any time soon that you need JAR.EXE to help solve such problems quickly and efficiently. But, since you posted the stack trace: it looks like the jar does not contain the classes your code depends upon, like SoapComponent. – Ingo Jul 23 '13 at 11:15
  • It contains the classes but however jar file cannot access them. Those classes work, when i run the program from eclipse. And it works like a charm. But when i extract it into jar. I recieve that error. As i told, different project with the same jre works like a charm. Why should this one require jar.exe while other one does not? It is what i did not really understand. Sorry for taking your time then. – Alasse Jul 23 '13 at 11:20
1

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.

ADTC
  • 8,999
  • 5
  • 68
  • 93
  • I have tried Extract and package options but the error did not change. I also tried the command line, but it did not work. Still the same error. :( – Alasse Jul 23 '13 at 10:24
  • You are attempting to run the Runnable JAR file you created using Eclipse, right? Maybe you can try full absolute paths for the jar files in `-cp` instead of relative paths. Also, if this is Linux, the separator character for `-cp` should be `:` instead of `;`. – ADTC Jul 23 '13 at 10:28
  • Hey thanks for quick reply again. Yes i am attempting to run the same runnable jar(I exported it freshly again and again). What do you mean by full absolute path? – Alasse Jul 23 '13 at 10:31
  • In Windows, the full absolute path would be (just an example to give you the idea) `C:/Users/Alasse/MyProject/sub/jar1.jar` and for Linux, it would be sth like `/usr/Alasse/myProject/sub/jar1.jar`.. btw, try forward slashes `/` instead of back slashes `\ `, I think that matters. – ADTC Jul 23 '13 at 10:35
  • Hey i tried that but nothing changed. If i type that like: java -cp sub\jar1.jar it gives information about that jar file. When i try to run them with my main jar, it gives the same error. However – Alasse Jul 23 '13 at 10:41
  • Can you post the stacktrace in the question? (Stacktrace is the full error report printed out when you try to run) – ADTC Jul 23 '13 at 10:45
  • too long to copy here. Editing post – Alasse Jul 23 '13 at 10:50
  • I still think it's the classpath configuration being incorrect. You can try this. Create a new project with just one class that has a main method that prints 'Hello World!'. Export this as runnable JAR and run it. If you can see 'Hello World!' printed out, definitely we will know this is a classpath issue and we can rule out problem with your project and work from there. _[Have to go. I will continue with you tomorrow. Sorry about that.]_ – ADTC Jul 23 '13 at 10:55
  • But like this we are not using the special libraries that i included in other project. Maybe error has happened because of them. – Alasse Jul 23 '13 at 11:05
  • 1
    @Alasse So yea, it should be classpath problem if your Eclipse project has no errors. Now in your Hello World project, attempt to import one of the library jars and call some constructor from it (example `SecurityProvider secman= new SecurityProvider(HOST,true);`). Do include some debug messages, and put the constructor call in a `try/catch(Exception e)` printing the stacktrace. Export this into a runnable JAR and attempt to run the JAR as you just did (try the different export options). – ADTC Jul 24 '13 at 01:15
  • 1
    mate however i solved the error using the steps above. Thank you for your interest. – Alasse Jul 24 '13 at 08:23
  • Glad to know my answer has helped. What can I do to expand it? Or rather, how did you fix your problem? What information did you find helpful beyond my answer? – ADTC Jul 24 '13 at 10:00
  • Extract required libraries into generated JAR Package required libraries into generated JAR options were really helpful. I used package option while doing that. But extract also worked. Besides these, i fixed it, by reorganizing references, and adding reference to project by using command line above. – Alasse Jul 24 '13 at 11:04
1

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138