Search code examples
javajarjavac

java command classpath issue


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


Solution

  • 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