0

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...?

3 Answers 3

0

When using -cp you need to specify jar names one by one. Using * does not usually add anything to the classpath. Good luck!

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

2 Comments

i have also tried to run java command as followed java -cp "test1.jar" Main but still same error
What about the jar where Test sits on, you appear to only have compiled main.java. Also, the jars need to be on local dir to be used this way, otherwise you need full path.
0

Everything is wrong.

  • The source code should be in a directory called com/example.
  • You should be in the directory containing 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.

Comments

-2

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

5 Comments

That doesn't make it work, and it isn't necessary. It can't work unless you specify the class to run correctly, and you haven't.
@EJP Can you please explain why java -cp ".\*"; Main works and java -cp ".\*" Main doesn't?
Neither works. Cannot reproduce. You will get what I did: the same NoClassDefFoundError you mentioned in your post, unless you've changed your code.
@EJP I have checked the posted code and the behavior does occur. I am using Java 1.8.0_73.
@RohitGupta, Wow thanks buddy. i got my solution. i have tried to resolved for two days. you saved my time. thanks again

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.