简体   繁体   English

在命令提示符下运行java程序

[英]Running a java program on command prompt

- Background information: - 背景资料:

I have recently started learning the basics of Java programming language. 我最近开始学习Java编程语言的基础知识。 To run my program at the command prompt, I downloaded the java development kit also known as JDK, and I set my windows 10 system path to: 为了在命令提示符下运行我的程序,我下载了Java开发工具包,也称为JDK,并将我的windows 10系统路径设置为:

C:\\Program Files\\Java\\jdk-9.0.1\\bin;C:\\Program Files\\Java\\jre-9.0.1\\bin C:\\ Program Files \\ Java \\ jdk-9.0.1 \\ bin; C:\\ Program Files \\ Java \\ jre-9.0.1 \\ bin

- Problem: - 问题:

After writing a simple Hello World program with the following format: 用以下格式编写简单的Hello World程序之后:

    class test{

        public static void main(String[] args){

            System.out.println("Hello World!");
        }
    }

and running it on command prompt using 并使用命令提示符运行它

javac test.java javac test.java

, and then writing ,然后写

java test java测试

the output says: 输出说:

Error: Main method is not static in class test, please define the main method as: 错误:主类方法在类测试中不是静态的,请将主方法定义为:
public static void main(String[] args) public static void main(String [] args)

I have tried compiling my simple program on an online Java compiler and everything works fine. 我试过在在线Java编译器上编译我的简单程序,一切正常。

- Edit: - 编辑:

As suggested using a Java decompiler. 正如使用Java反编译器所建议的那样。 I used http://www.javadecompilers.com/result and the output was: 我使用了http://www.javadecompilers.com/result ,输出结果如下:

    import java.io.PrintStream;

      public class test { 
          public test() {} public void main(String[] paramArrayOfString) { 
               System.out.println("Hello World!"); 
          }
      }

- Question: - 题:

Where is the problem coming from? 问题来自哪里? How can I fix it? 我该如何解决?

Solution: 解:

I was using "Sublime Text 3" when writing and saving my test.java program. 在编写和保存我的test.java程序时,我使用的是“Sublime Text 3”。 @Silvio Mayolo suggested using a java decompiler to find out the problem, and I noticed that when saving my program in Sublime, the static gets deleted in test.java file for some reason. @Silvio Mayolo建议使用java反编译器找出问题,我注意到在Sublime中保存我的程序时,由于某种原因,静态会在test.java文件中被删除。 Then I did the following steps: 然后我做了以下步骤:

  1. I closed sublime text 3 我关闭了崇高的文字3
  2. I opened my test.java file using notepad. 我用记事本打开了我的test.java文件。 I realized that static was missing after public, so it was public void main(String args){}. 我意识到静态在public之后就丢失了,所以它是public void main(String args){}。
  3. I added static in notepad, so it became public static void main(String[] args){} 我在记事本中添加了static,所以它变成了public static void main(String [] args){}
  4. I saved the file again in notepad. 我在记事本中再次保存了文件。
  5. I ran javac test.java in command prompt, and then java test, and I got my Hello World output. 我在命令提示符下运行了javac test.java,然后是java test,我得到了Hello World输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM