`java -cp company.HelloWorld`是否可以编译并运行Java应用程序?我猜不是.我只需要再次确认 [英] Does `java -cp company.HelloWorld` compile and run an java application? I guess not. I just need a double confirmation
问题描述
我关注此帖子和此
I following this post and this post to build an java app.
这是我的 company/HelloWorld.java
package company;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
通常,运行Java程序需要2个步骤:
Classically, running a java program needs 2 steps:
- javac文件,例如
javac company/HelloWorld.java
- java类文件,例如
java company.HelloWorld
正如预期的那样,它给了我世界你好".
which gave me "Hello, World", as expected.
尽管我的环境似乎无法识别此命令,但 java -cp
似乎可以批量完成上述两个步骤
It seems java -cp
could do the 2 steps above in a batch, although it seems that my env does not recognize this command
java -cp company.HelloWorld
并输出
用法:java [-options]类[args ...]
Usage: java [-options] class [args...]
根据@hayrettinm的回答,这是另一个版本.
this is another version, according to @hayrettinm's answer.
package company.HelloWorld;
也不起作用.我想念什么?
didn't work either. what am I missing?
Why do I thought java -cp
could do compile and run? this post gives this example
PS C:\Lecture_java\Lecture001\Hello> & 'C:\Users\ubuntu\.vscode\extensions\vscjava.vscode-java-debug-0.23.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-13.0.1\bin\java' '-Dfile.encoding=UTF-8' '-cp' 'C:\Lecture_java\Lecture001\Hello\bin' 'app.App'
实验结束后,我猜想 java -cp company.HelloWorld
无法编译运行,它只能运行Java应用程序.有人可以给一个再次确认.
after the experiment, I guess java -cp company.HelloWorld
could not compile and run, it just run an java application. Could someone give a
double confirmation.
推荐答案
java -cp不会执行您编写的内容.
java -cp doesn't do what you write.
javac可执行文件是将您的类编译为字节码的编译器,Java可执行文件基本上是可以理解您已编译的字节码的运行时.
javac executable is the compiler that compiles your class to bytecode, java executable is basically the runtime that understands the bytecode you have compiled.
此外,
如果要使用软件包 com.company.HelloWorld
您必须在您的班级中像这样编写包清理:
If you want to use package com.company.HelloWorld
you have to write package decleration like this in your class:
package com.company.HelloWorld;
这篇关于`java -cp company.HelloWorld`是否可以编译并运行Java应用程序?我猜不是.我只需要再次确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!