Java在命令提示符下运行程序:找不到或加载主类 [英] Java running a program at command prompt: could not find or load main class

查看:159
本文介绍了Java在命令提示符下运行程序:找不到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习如何使用Windows中只使用命令行来编译和运行。这是从根开始的目录树:

I am trying to learn how to compile and run using only command lines in Windows. Here is the tree of the directories starting 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 thing is that I am now confused as to how to run the program. I tried two things, where both returned different errors.

第一次尝试:

java -classpath classes StudentMasterList

返回:

Error: Could not find or load main class StudentMasterList


b $ b

第二次尝试:

2nd try:

java -classpath classes/com/wat/sampleapp StudentMasterList

返回:

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 got confused after the next tries, any help on how i should run the main class?

更新:我应该在 ActivityOne 级别运行我的所有命令。

Update: I should run all my commands at the ActivityOne level.

推荐答案

正确的方式是

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

换句话说,您将顶级目录添加到类路径,然后使用完全您的Java类的限定名称。

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

这篇关于Java在命令提示符下运行程序:找不到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆