Java running program at command prompt: main class not found or loaded

I'm trying to learn how to compile and run using only the command line in windows This is the directory tree from the root:

D:
 ActivityOne
     - classes
         - com
           -wat
             -sampleapp
                -students
                    StudentE.class
                 StudentMasterList.class (Main)
     - src
         -com
           -wat
             -sampleapp
                -students
                    StudentE.java
                 StudentMasterList.java (Main)

The problem is that I'm confused about how to run the program now I tried two things, both of which returned different errors

First attempt:

java -classpath classes StudentMasterList

Back:

Error: Could not find or load main class StudentMasterList

Second attempt:

java -classpath classes/com/wat/sampleapp StudentMasterList

Back:

Exception in thread "main" java.lang.NoClassDefFoundError: StudentMasterList (wrongname: com/wat/sam
pleapp/StudentMasterList)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(UnkNown Source)
at java.security.SecureClassLoader.defineClass(UnkNown Source)
at java.net.urlclassloader.defineClass(UnkNown Source)
at java.net.urlclassloader.access$100(UnkNown Source)
at java.net.urlclassloader$1.run(UnkNown Source)
at java.net.urlclassloader$1.run(UnkNown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    at sun.launcher.LauncherHelper.checkAndLoadMain(UnkNown Source)

I'm confused after the next attempt. Any help on how to run the main class?

Update: I should run all commands at the activityone level

Solution

The correct way is

java -classpath D:\ActivityOne\classes com.wat.sampleapp.StudentMasterList

In other words, you add the top - level directory to the classpath and use the fully qualified name of the Java class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>