英文:
Unable to execute simple java file from windows command line
问题
无法从Windows命令行执行此代码。
- 我编写的代码如下:
package sample;
public class PrivateMain{
public static void main(String[] args){
System.out.println("This is from main");
}
}
- 我已经使用下面的命令设置了类路径,但仍然收到错误消息"Could not find or load main class"。我漏掉了哪些步骤吗?
G:\javaPract>javac PrivateMain.java
G:\javaPract>java PrivateMain
Error: Could not find or load main class PrivateMain
G:\javaPract>java sample.PrivateMain
Error: Could not find or load main class sample.PrivateMain
G:\javaPract>set CLASSPATH=%CLASSPATH%;.
G:\javaPract>echo %CLASSPATH%
G:\javaPract;.
G:\javaPract>javac PrivateMain.java
G:\javaPract>java PrivateMain
Error: Could not find or load main class PrivateMain
G:\javaPract>java sample.PrivateMain
Error: Could not find or load main class sample.PrivateMain
G:\javaPract>
- G:\javaPract目录中的文件内容:
G:\javaPract>dir
Volume in drive G is Learning
Volume Serial Number is 008F-C257
Directory of G:\javaPract
03/21/2023 08:04 AM <DIR> .
03/21/2023 08:04 AM <DIR> ..
05/17/2023 04:35 PM 280 PrivateMain.java
05/17/2023 04:36 PM 688 PrivateMain.class
英文:
Unable to execute this code from windows command line
- I wrote the code as :
package sample;
public class PrivateMain{
public static void main(String[] args){
System.out.println("This is from main");
}
}
- I have set the classpath using below commands, but still receiving error as "Could not find or load main class". Am i missing any steps?
G:\javaPract>javac PrivateMain.java
G:\javaPract>java PrivateMain
Error: Could not find or load main class PrivateMain
G:\javaPract>java sample.PrivateMain
Error: Could not find or load main class sample.PrivateMain
G:\javaPract>set CLASSPATH=%CLASSPATH%;.
G:\javaPract>echo %CLASSPATH%
G:\javaPract;.
G:\javaPract>javac PrivateMain.java
G:\javaPract>java PrivateMain
Error: Could not find or load main class PrivateMain
G:\javaPract>java sample.PrivateMain
Error: Could not find or load main class sample.PrivateMain
G:\javaPract>
- file contents on G:\javaPract directory:
G:\javaPract>dir
Volume in drive G is Learning
Volume Serial Number is 008F-C257
Directory of G:\javaPract
03/21/2023 08:04 AM <DIR> .
03/21/2023 08:04 AM <DIR> ..
05/17/2023 04:35 PM 280 PrivateMain.java
05/17/2023 04:36 PM 688 PrivateMain.class
答案1
得分: 2
java PrivateMain
does not work
这是因为你的类并不叫做这个。它叫做 sample.PrivateMain
。如果你在“我会提供类名”的模式下使用 java
(即不使用 -jar
或 -jmod
启动选项),它必须是完全限定的。
java sample.PrivateMain
does not work
类名会被转换成路径,将点转换成斜杠并附加 .class
。换句话说,如果你运行 java sample.PrivateMain
,Java 会启动,并检查类路径中的每个目录,将 sample/PrivateMain.class
附加到它上面。这不会找到你的类,因为你的类不在 sample/PrivateMain
中,而在 ./PrivateMain
中,这不是 Java 会查找的地方。
如何修复
对于这样的临时项目,去掉 package
声明。顺便说一下,你可以直接运行 java MyMain.java
,不需要涉及 javac
。一旦你升级到更复杂的项目,涉及多个源文件,尤其是多个包时,放弃这个命令行的 javac
和 java
,而使用像 Maven 或 Gradle 这样的构建系统。
如果你必须出于某种奇怪的原因使用包声明,请按以下方式设置项目:
cd G:\javaPract
mkdir sample
cd sample
# 在这里放置你的 PrivateMain.java 文件
javac PrivateMain.java
cd ..
java -cp . sample.PrivateMain
关键点是,文件必须位于 G:\javaPract\sample\PrivateMain.class
,而 G:\javaPract
(而不是 G:\javaPract\sample
!)必须在类路径上。然后运行 java sample.PrivateMain
。
一般情况下,不要使用 CLASSPATH
,只需使用 -cp
参数来指定。CLASSPATH
是一个全局变量,作为一般规则,将计算机限制为只能运行一个 Java 应用程序是不合理的。
英文:
java PrivateMain
does not work
That's because your class isn't called that. It's called sample.PrivateMain
. If you use java
in a 'I shall give you the name of my class' mode (i.e. not via the -jar
or -jmod
launch options), it has to be fully qualified.
java sample.PrivateMain
does not work
class names are turned into paths by turning dots into slashes and appending .class
. In other words, if you run java sample.PrivateMain
, java starts, and checks every dir in the classpath by appending sample/PrivateMain.class
to it. This doesn't find your class because your class isn't in sample/PrivateMain - it's in ./PrivateMain which isn't where java is going to look at all.
How to fix it
For throwaway projects like this, get rid of the package
declaration. While you're at it, you can just run java MyMain.java
- no need to involve javac
. Once you graduate to more serious projects with multiple source files, and especially, multiple packages, ditch this command line javac
and java
stuff and use a build system such as maven or gradle.
If you must, for some strange reason, use a package statement, set up your project like so:
cd G:\javaPract
mkdir sample
cd sample
# put your PrivateMain.java file here
javac PrivateMain.java
cd ..
java -cp . sample.PrivateMain
key point is, file has to be in G:\javaPract\sample\PrivateMain.class
, and G:\javaPract
(not G:\javaPract\sample
!) has to be on the classpath. Then run java sample.PrivateMain
.
Generally, do not use CLASSPATH
, just specify with the -cp
parameter. CLASSPATH
is a global variable and as a general rule it's bizarre to limit your computer to only run one java application ever.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论