Running a java program on command prompt
3906 观看
1回复
- Background information:
I have recently started learning the basics of Java programming language. 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:
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:
class test{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
and running it on command prompt using
javac test.java
, and then writing
java test
the output says:
Error: Main method is not static in class test, please define the main method as:
public static void main(String[] args)
I have tried compiling my simple program on an online Java compiler and everything works fine.
- Edit:
As suggested using a Java decompiler. I used http://www.javadecompilers.com/result and the output was:
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?
作者: Kourosh 的来源 发布者: 2019 年 11 月 20 日回应 (1)
0像
Solution:
I was using "Sublime Text 3" when writing and saving my test.java program. @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. Then I did the following steps:
- I closed sublime text 3
- I opened my test.java file using notepad. I realized that static was missing after public, so it was public void main(String args){}.
- I added static in notepad, so it became public static void main(String[] args){}
- I saved the file again in notepad.
- I ran javac test.java in command prompt, and then java test, and I got my Hello World output.
来自类别的问题 :
- java Java和C#中的int和Integer有什么区别?
- java 如何在Java中确定路由器/网关的IP?
- java 根据XSD文件验证XML文件的最佳方法是什么?
- java 如何整理整数除法的结果?
- java 将List <Integer>转换为List <String>
- windows-10 如何自动提升我的批处理文件,以便在需要时从UAC管理员权限请求?
- windows-10 用于IIS的PHP管理器无法安装
- windows-10 在Windows 10上导入win32api时出现DLL错误
- windows-10 使用VS2015和Windows 10的uap应用左上角的两个数字是什么?
- windows-10 未加载Windows 10和IE 11 ActiveX
- command-prompt 在命令提示符下编译/执行C#源文件
- command-prompt 我可以在bat文件中屏蔽输入文本吗
- command-prompt javac无法在Windows命令提示符下工作
- command-prompt 如何在Ruby中使用RSpec获取Windows命令提示符?
- command-prompt 从Windows应用程序执行命令行
- java-deployment-toolkit Running a java program on command prompt