简体   繁体   English

无法从命令行运行 java 程序

[英]Unable to run java program from commandline

I am having serious trouble with running a java program from the commandline.我在从命令行运行 java 程序时遇到了严重的问题。 I feel I am so close to having figured out whats wrong, but I'm just at a loss, and I need help.我觉得我已经很接近弄清楚出了什么问题,但我只是不知所措,我需要帮助。

I'm trying to run a simple Hello World program (in VSCode on windows, using bash terminal) with the following commands:我正在尝试使用以下命令运行一个简单的 Hello World 程序(在 windows 上的 VSCode 中,使用 bash 终端):

javac Hello.java

java Hello

This simply doesn't work, however, writing the full path to the file does work, so something like this works.这根本行不通,但是,写入文件的完整路径确实有效,所以像这样的事情是可行的。

javac -cp c//path//to//file// Hello.java

java -cp c//path//to//file// Hello

Also, running the file with F5 also works fine.此外,使用 F5 运行该文件也可以正常工作。 I would like to run my program with command line arguments, and not have to write the entire path to the file every time, as it is very tedious.我想用命令行 arguments 运行我的程序,而不必每次都将整个路径写入文件,因为这非常乏味。

My PATH and CLASSPATH variables are as follows:我的PATH和CLASSPATH变量如下:

PATH: C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot\bin

CLASSPATH: C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot\bin

Could anyone give me some insight as to what is wrong?谁能给我一些关于什么地方出了问题的见解? The closest solution I've found to reduce the amount I have to manually write is to save a variable in my.bashrc to my current working directory, making the following command possible:我发现减少我必须手动编写的数量的最接近的解决方案是将 my.bashrc 中的变量保存到我当前的工作目录,使以下命令成为可能:

java "$cwd"Hello

Any help is greatly appreciated.任何帮助是极大的赞赏。

Your CLASSPATH is broken.你的 CLASSPATH 坏了。

CLASSPATH as a concept is where java class files (or jars with the class files inside them) live. CLASSPATH 作为一个概念是java class 文件(或 jars 和其中的 class 文件)所在的位置。 Not where the java binaries are (java generally knows where they are. For the few tools that do not, JAVA_HOME is for that, and you provide the 'home dir' (the parent of that bin dir).不是java 二进制文件所在的位置(java 通常知道它们在哪里。对于少数不知道的工具, JAVA_HOME用于此,并且您提供“主目录”(该bin目录的父目录)。

Java knows where to find its own core classes (such as java.lang.String and co), these are provided by the bootclasspath. Java 知道在哪里可以找到自己的核心类(比如java.lang.String和 co),这些都是由 bootclasspath 提供的。

Generally, you don't want to use CLASSPATH as an environment variable in the first place: You can have more than one java project on a single computer, with completely different needs.通常,您一开始就不想使用CLASSPATH作为环境变量:您可以在一台计算机上拥有多个 java 项目,它们的需求完全不同。 That's why IDEs, build tools, and java -jar completely ignore that variable.这就是 IDE、构建工具和java -jar完全忽略该变量的原因。

However, it is the answer for making just plain ole javac *.java; java MyClass但是,它制作普通 ole javac *.java; java MyClass javac *.java; java MyClass work. javac *.java; java MyClass工作。 Specifically, you should set CLASSPATH to: "."具体来说,您应该将CLASSPATH设置为:"."

That's it.就是这样。 Just dot: The current directory.只是点:当前目录。 Once you've done this, you can simply type:完成此操作后,您只需键入:

javac MyApp.java
java MyApp

Note that with modern java versions, you can actually just write java MyApp.java ;请注意,对于现代 java 版本,您实际上可以只编写java MyApp.java java will compile and run your code in one go. This will fail to work real fast (when you involve dependencies and multiple packages), but for the most academically simple case of a single source file, that works fine too. java 将在一个 go 中编译和运行您的代码。这将无法真正快速地工作(当您涉及依赖项和多个包时),但对于单个源文件的最学术简单的情况,它也可以正常工作。

Note that "$PWD" just works, out of the box.请注意, "$PWD"开箱即用。

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

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