Could not find or load main class package.ClassName

Aman :

I have two packages and classes First package

D:\User\java\java_programs\mypackage\A.java

package mypackage;
public class A
{
    public void msg(String name)
    {
        System.out.println("Hello "+name);
    }
    public static void main (String args[])
    {
        System.out.println("hello User");
    }
}   

And There is another class B in C:\package1\B.java

package package1;
import mypackage.A;
public class B
{
    public static void main (String args[])
    {
        A obj = new A();
        obj.msg("User");
    }
}   

I compiled

C:\package1>javac -cp d:\User\java\java_programs\ B.java

And It created the class files for both A.java and B.java

But I cannot run my code

I tried

java -cp d:\User\java\java_programs\ package1.B

But Error

Error: Could not find or load main class package1.B

rsp :

You need to read up on how the Java classpath works, for package1 your root of the classpath would be C:\. If you add C:\ to the -cp argument it will do what you need.

The command line becomes

java -cp C:\;D:\User\java\java_programs package1.B 

The Java classpath consists of the root folders under which your package structures can be found. (Jar files contain the package folder structure starting from their root.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238791&siteId=1