Classpath seems correct but JVM still could not find or load main class

一笑奈何 提交于 2019-12-12 21:51:30

问题


I am relatively new to programming and java and am trying to learn how to create a user-defined package from the command-line. I get the following: Error: Could not find or load main class TestPhone. I've reviewed posts on this type of error including the well-commented post here. The post lists 4 steps that the JVM goes through to run a java program:

  1. Search for the compiled version of the class.
  2. Load the class.
  3. Check that the class has a main method with signature static void main(String[]).
  4. Call that method passing it the command line arguments as a String[].

Apparently, my JVM can't find my TestPhone.class for some reason I am yet to figure out. Here's my directory structure:

My classpath is set as follows:

My classes contain simple codes from Mala Gupta to test accessibility of class variables:

package mobile;
class Phone {
    static boolean softKeyboard = true;
}


package mobile;
class TestPhone {
    public static void main (String[] args) {
        Phone.softKeyboard = false;

        Phone p1 = new Phone();
        Phone p2 = new Phone();

        System.out.println(p1.softKeyboard);
        System.out.println(p2.softKeyboard);

        p1.softKeyboard = true;

        System.out.println(p1.softKeyboard);
        System.out.println(p2.softKeyboard);
    }
}

Any idea why it doesn't find my classes? Many thanks.

System specs: Java version 1.8.0 Javac 1.8.0 Win 7 on 32-bit OS


回答1:


The java application launcher, java, expects the fully qualified name of the class, mobile.TestPhone. The ../mobile directory need not be in the classpath.




回答2:


You are in the myJavaProject folder in cmd.

Try to go to c:\myJavaProject\mobile then it should work because your class is in mobile not in myJavaProject folder



来源:https://stackoverflow.com/questions/26852459/classpath-seems-correct-but-jvm-still-could-not-find-or-load-main-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!