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

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

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

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

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

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

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

C:\Users\user_name\Desktop

Then I check if the file exists and it does.

dir

Then I try to compile the file by typing

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.

dir

Then I attempt to run the java interpreter

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

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

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

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

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

C:\Users\user_name\Desktop

Then I check if the file exists and it does.

dir

Then I try to compile the file by typing

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.

dir

Then I attempt to run the java interpreter

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

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

示例:

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

然后,你应该编译它:

javac FileName.java

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

javac -version

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

java -version

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

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

英文:

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

Example:

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

Then you should compile it

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:

javac -version

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

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:

确定