I am having strange issue in this simple scenario.
I am having a jar file which contains following class:
package com.example;
public class Test{
public void perform(){
System.out.println("Performing testing one");
}
}
And I have created a Main class to call the perform method as followed:
import com.example.Test;
public class Main{
public static void main(String[] args) {
Test test=new Test();
test.perform();
}
}
I have put both the jar and Main.java file in same folder and successfully compiled the Main.java file using following command:
javac -cp ".\\*" Main.java
But when I am trying to run the Main class using following command:
java -cp ".\\*" Main
It gives following error:
Error: Could not find or load main class Main
If I try to run Main class without -cp
argument it gives following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/example/Test at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: com.example.Test 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) ... 1 more
The syntax is correct then what I am doing wrong here...?
When using -cp you need to specify jar names one by one. Using * does not usually add anything to the classpath. Good luck!
Everything is wrong.
com/example
. com
. The compiler command is
javac com/example/Main.java
The execute command is
java com.example.Main
You don't need a JAR file with only one class in it.
After a lot of experiment and searches on net, I have finally found that we need to put ;
after classpath to make it work. So following command does run the Main class:
java -cp ".\\*"; Main
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.