我无法从CMD运行Java程序,我的路径可能有问题。

huangapple go评论93阅读模式
英文:

I can't run java program from the CMD my paths may be we

问题

I'm trying to run Welcome.java program from the cmd

  1. public class Welcome {
  2. public static void main(String[] args) {
  3. System.out.println("Welcome to Java!");
  4. }
  5. }

Before I mention what I have been trying, when I type where java into cmd I get two different paths

  1. C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
  2. D:\java\jdk\bin\java.exe

First I set my current directory to where welcome.java is.

  1. C:\Users\user_name\Desktop

Then I check if the file exists and it does.

  1. dir

Then I try to compile the file by typing

  1. javac Welcome.java

Then I check again to see if welcome.class has been created, I think the issue might be here since I don't see any welcome.class file. I don't know if this is because something is wrong with my paths.

  1. dir

Then I attempt to run the java interpreter

  1. java Welcome

After this, I don't see any output, it just returns to the next system prompt.

英文:

I'm trying to run Welcome.java program from the cmd

  1. public class Welcome {
  2. public static void main(String[] args) {
  3. System.out.println("Welcome to Java!");
  4. }
  5. }

Before I mention what I have been trying, when I type where java into cmd I get two different paths

  1. C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
  2. D:\java\jdk\bin\java.exe

First I set my current directory to where welcome.java is.

  1. C:\Users\user_name\Desktop

Then I check if the file exists and it does.

  1. dir

Then I try to compile the file by typing

  1. javac Welcome.java

Then I check again to see if welcome.class has been created, I think the issue might be here since I dont see any welcome.class file. I don't know if this is because something is wrong with my paths.

  1. dir

Then I attempt to run the java interpreter

  1. java Welcome

After this I don't see any output, it just returns to the next system prompt.

答案1

得分: 2

Check the JAVA_HOME variable and compare to the one specified in your ide.

Change the name of your welcome.java with Welcome.java.

英文:

Check the JAVA_HOME variable and compare to the one specified in your ide.

Change the name of your welcome.java with Welcome.java

答案2

得分: 2

为了执行这两个命令,您必须配置您的系统变量。

  1. 您必须将 D:\java\jdk\bin 或 %JAVA_HOME%\bin 添加到您的路径变量中。

  2. 您应该创建一个新的系统变量来配置 JAVA_HOME:JAVA_HOME=D:\java\jdk

要添加这些变量,您可以按照此链接操作:https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html#GUID-DD6F9982-60D5-48F6-8270-A27EC53807D0

您必须重新启动您的计算机以加载这些更改。

然后,执行:

javac Welcome.java

这个命令应该在同一目录下创建一个 Welcome.class 文件。
最后,您可以执行以下命令:

java Welcome

英文:

To execute these 2 command, you must to configure your system variables.

  1. You must to add into your path variable D:\java\jdk\bin or %JAVA_HOME%\bin.

  2. You should configure JAVA_HOME creating one new system variable: JAVA_HOME=D:\java\jdk

To add this variables you can follow this link: https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html#GUID-DD6F9982-60D5-48F6-8270-A27EC53807D0

You have to restart your PC to load the changes.

Then, execute:

javac Welcome.java

This command should create one Welcome.class in the same directory.
Fianlly, You can execute the command

java Welcome

我无法从CMD运行Java程序,我的路径可能有问题。

答案3

得分: 1

你应该让类名和文件名相同(区分大小写)。

示例:

  1. public class FileName {
  2. public static void main(String[] args) {
  3. System.out.println("Welcome to Java!");
  4. }
  5. }

然后,你应该编译它:

  1. javac FileName.java

检查环境变量是否设置为Java平台的正确路径。你可以通过运行以下命令来执行此操作:

  1. javac -version

"javac -version" 命令检查Java编译器版本(javac)

  1. java -version

"java -version" 命令检查Java运行时环境(JRE)版本

或者只需键入 java,它应该会给你关于如何使用 Java 命令 的消息。

英文:

You should have the class name and the file name the same (case sensitive).

Example:

  1. public class FileName{
  2. public static void main(String[] args) {
  3. System.out.println("Welcome to Java!");
  4. } }

Then you should compile it

  1. javac FileName.java

Check where the environment variables are set to the correct path of the Java platform. You can do this by running these commands:

  1. javac -version

"javac -version" command checks the Java Compiler version (javac)

  1. java -version

"java -version" command checks the Java Runtime Environment (JRE) version

Or just type java and it should give you a message about how to use the Java command.

huangapple
  • 本文由 发表于 2023年5月26日 00:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定