简体   繁体   English

Java HelloWorld程序运行

[英]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" ,因为:

  1. The class may have not compiled correctly. 该类可能未正确编译。
  2. The compiled class is not available on the path from where you are trying to run it. 尝试运行它的路径上没有编译的类。
  3. Proper classpath is not set for compiling and running the java classes. 没有为编译和运行Java类设置正确的类路径。

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.

相关问题 当我从 Codename One HelloWorld 教程运行 Codename One HelloWorld Java 程序时,出现错误 - When I run the Codename One HelloWorld Java Program from the Codename One HelloWorld Tutorial, I get an error 如何使用 Maven californium 运行 helloWorld - Java - How to run helloWorld using Maven californium - Java 在Linux上编译并运行HelloWorld.java - Compile and Run HelloWorld.java on linux 使用 Heroku Scheduler 运行一个简单的 Java HelloWorld - Run a simple Java HelloWorld with Heroku Scheduler 如何在不使用Eclipse的情况下在Spring和Maven中运行HelloWorld程序 - How to run HelloWorld program in spring and maven without using Eclipse 尝试使用javac编译.java helloworld程序时出错 - Error when trying to compile .java helloworld program using javac LinkageError 在我使用 VS Code 的第一个“HelloWorld”Java 程序中 - LinkageError in my first "HelloWorld" Java program using VS Code JNI:为简单的HelloWorld程序获取java.lang.UnsatisfiedLinkError - JNI : Getting java.lang.UnsatisfiedLinkError for simple HelloWorld Program 您可以为Java程序使用类似“ HELLOWorld.class”的名称吗? - Can you use a name like “HELLOWorld.class” for a java program? 有没有办法动态重启循环打印输出的HelloWorld Java程序? - Is there a way to dynamically restart a HelloWorld Java program which loops print outputs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM