[英]Java HelloWorld program run
I tried to run my basic HelloWorld.class file from my terminal. 我试图从终端运行基本的HelloWorld.class文件。
I use the following input: 我使用以下输入:
Java HelloWorld.class
But it says: 但它说:
Error: Could not find or load "HelloWorld.class"
I have tried giving it a directory but it doesn't work. 我尝试给它提供一个目录,但是它不起作用。
because you didn't compiled or run it successufully.you should use 因为您没有成功编译或运行它。您应该使用
javac HelloWorld.java
to complile and 顺应
then use 然后使用
java HelloWorld
to run it. 运行它。 plz check this tutorial http://introcs.cs.princeton.edu/java/11hello/ 请检查本教程http://introcs.cs.princeton.edu/java/11hello/
The class should be (Executable Class should definitely contain the main method with same declaration as below) 该类应为(可执行类绝对应包含具有以下声明的main方法)
Class MyClassName
{
// Methods here
public static void main (String args[])
{
// Code here
}
}
To Compile , it should be: 要进行编译 ,应为:
javac MyClassName.java
On successful compilation, MyClassName.class
would be generated in your folder. 成功编译后,将在您的文件夹中生成MyClassName.class
。
To run , it should be 要运行 ,应该是
java MyClassName
In case your java is in say D:/JavaWorkDir/src
, You need to compile and run from the folder D:/JavaWorkDir/src
. 如果您的Java位于D:/JavaWorkDir/src
,则需要从文件夹D:/JavaWorkDir/src
进行编译和运行。 Also Ensure that your classpath is set appropiately. 另外,请确保正确设置了类路径。
You are receiving this error because you shouldn't include the .class
when you run the compiled file. 您收到此错误是因为在运行编译文件时不应包含.class
。
After you've compiled: 编译后:
javac HelloWorld.java
run using: 运行使用:
java HelloWorld
(don't do: java HelloWorld.class
) (不要这样做: java HelloWorld.class
)
Run it as Java HelloWorld
and not like Java HelloWorld.class
. 作为Java HelloWorld
运行它,而不像Java HelloWorld.class
运行它。
The error Error: Could not find or load "HelloWorld.class"
is occuring because: 错误Error: Could not find or load "HelloWorld.class"
,因为:
Whenever you write a Java Program named HelloWorld
, you must compile it as: 每当编写名为HelloWorld
的Java程序时,都必须将其编译为:
javac HelloWorld.java
Once the HelloWorld.class
class file generated in the same directory where you have your java file, compiled by the compiler, you can run it from console as: 一旦HelloWorld.class
类文件在Java文件所在的目录中生成,并由编译器编译,则可以从控制台运行它,如下所示:
java HelloWorld
You don't run it as 您不会像
java HelloWorld.class
but 但
java HelloWorld
There is no need of .class
extension. 不需要.class
扩展名。
But note you always have to use fully qualified name
. 但是请注意,您始终必须使用fully qualified name
。 So if your class resides in some package say myPackage
then you need to run 因此,如果您的类驻留在某个包中,例如myPackage
则需要运行
java myPackage.HelloWorld
如果您想尝试世界,您也可以尝试在NetBeans应用程序和Jdoodle.com上运行它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.