0

I created a jar file in windows & copied it to linux and tried to execute with the following command:

java -jar Test.jar com.dcat2.messaging.datatransfer.Test

I am getting an exception as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Test. Program will exit.

I am executing this command in the same location where i have the jar file.

Here is my java code:

package com.dcat2.messaging.datatransfer;

public class Test {
public static void main(String[] args) {
    System.out.println("Test App : " + args[0]);

}
}

The jar -tf command's output is as follows:

jar -tf Test.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/dcat2/
com/dcat2/messaging/
com/dcat2/messaging/datatransfer/
com/dcat2/messaging/datatransfer/Test.class

Can anyone help please?

0

1 Answer 1

1
java -cp Test.jar com.dcat2.messaging.datatransfer.Test

The -jar option launches the class that is indicated as the main class in the manifest of the jar file (and which must be the Test class, in the default package, in your case).

The -cp option sets the classpath to use for the app which, in this case, only contains your test jar.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much ...It worked :) One more doubt...So, If I am executing this command from some another location, how should i set the classpath and execute?
java -cp /path/to/the/Test.jar com.dcat2.messaging.datatransfer.Test. The path can be absolute or relative.
BTW, it is possible , using Eclipse, to package the jar with its dependencies so your jar will contain all the other jars it needs. and then you can run it with no '-cp'
I tried that... But it says "Could not find or load main class Test.jar"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.