简体   繁体   English

java.exe在当前工作目录中似乎找不到.java文件

[英]java.exe can't seem to find a .java file in the current working directory

I installed Java on Windows 10. It installed here: C:\\Program Files\\Java\\jdk-10.0.1 . 我在Windows 10上安装了Java。它安装在这里: C:\\Program Files\\Java\\jdk-10.0.1 I made sure the path had been modified to include the bin folder and java.exe . 我确保路径已被修改为包括bin文件夹和java.exe I can type 'java' in a cmd window and get the java usage message. 我可以在cmd窗口中键入“ java”并获取Java使用情况消息。 So, that works. 因此,这可行。

Then I installed Eclipse. 然后我安装了Eclipse。 I went through the tutorial section walkthru to create the HelloWorld program. 我遍历了教程部分的“创建” HelloWorld程序。 And it runs in Eclipse. 它在Eclipse中运行。 Here it is: 这里是:

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello World!");
    }

}

Now that I have that set up correctly, I wanted to try running it without Eclipse. 现在我已经正确设置了,我想尝试在没有Eclipse的情况下运行它。 So, I copied the HelloWorld.java source file from the Eclipse work space to a folder I created under Documents, C:\\Users\\kris4\\Documents\\java . 因此,我将Eclipse工作区中的HelloWorld.java源文件复制到了我在Documents, C:\\Users\\kris4\\Documents\\java下创建的文件夹。

I open a cmd window, cd to C:\\Users\\kris4\\Documents\\java , and do a dir : 我打开一个cmd窗口,CD到C:\\Users\\kris4\\Documents\\java ,然后执行dir

Volume in drive C has no label.
 Volume Serial Number is A656-1FA0

 Directory of C:\Users\kris4\Documents\java

05/24/2018  03:22 PM    <DIR>          .
05/24/2018  03:22 PM    <DIR>          ..
05/24/2018  11:20 AM               160 HelloWorld.java
               1 File(s)            160 bytes
               2 Dir(s)  116,141,473,792 bytes free

There it is... 那里...

But, when I execute, java HelloWorld.java I get this: 但是,当我执行java HelloWorld.java时,得到以下信息:

Error: Could not find or load main class HelloWorld.java
Caused by: java.lang.ClassNotFoundException: HelloWorld.java

The file is right there in the working directory, and the file is a copy of a working Eclipse workspace file. 该文件位于工作目录中,并且是工作Eclipse工作区文件的副本。 WTH! WTH! Looking on the internet, I thought java needs CLASSPATH to find the source, so I defined a variable CLASSPATH to be the directory HelloWorld.java resides in. Still no luck. 在Internet上查看时,我认为Java需要CLASSPATH来查找源,因此我将变量CLASSPATH定义为HelloWorld.java所在的目录。

Obviously, being new to this, I'm missing something very basic in the cmdline environment that should be there, but I honestly expected java.exe to look in the current working directory for a file given as an argument, like any other program. 显然,这是我的新手,我在cmdline环境中缺少一些非常基本的东西,但是老实说,我希望java.exe在当前工作目录中查找作为自变量指定的文件,就像其他程序一样。

Can one of you folks help me out? 你们中的一个可以帮助我吗?

Use: 采用:

javac HelloWorld.java

not just 不只是

java HelloWorld.java

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

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