简体   繁体   English

在命令提示符错误中运行一个简单的 Java 程序

[英]Running a simple Java Program in Command Prompt error

I am trying to run my Example2.class program in windows 7 64-bit command prompt.我正在尝试在 Windows 7 64 位命令提示符下运行我的 Example2.class 程序。 I used command prompt already to compile the program, but when type: "java Example2" it gives me an error saying could not find or load main class example2.我已经使用命令提示符来编译程序,但是当输入:“java Example2”时,它给了我一个错误,说找不到或加载主类 example2。 How do I set the right path to my file so that it can find it?如何为我的文件设置正确的路径以便它可以找到它? Thanks谢谢

You need to give it the full package name, and (unless you change the class path) you need to be in the right directory.您需要为其提供完整的包名称,并且(除非您更改类路径)您需要位于正确的目录中。 If the full package name is如果完整的包名称是

com.something.Example2

then you'd expect the compiler to produce a file like this:那么你会期望编译器生成这样的文件:

com/something/Example2.class

If you make sure you're in the directory immediately above com (ie, you can see com when you do a directory listing), then you can run it with如果您确保您位于com正上方的目录中(即,您可以在执行目录列表时看到com ),那么您可以使用以下命令运行它

java com.something.Example2

Note that it's case sensitive.请注意,它区分大小写。

If you used the default package (ie, the full class name really just is Example2 ) then you need to be in the directory containing Example2.class , and then you run如果您使用默认包(即,完整的类名实际上只是Example2 ),那么您需要在包含Example2.class的目录中,然后运行

java Example2

But using the default package is discouraged.但是不鼓励使用默认包。

The biggest thing you could do to help yourself out is to use an IDE (Eclipse or NetBeans are the most commonly used ones).您可以做的最重要的事情是使用 IDE(Eclipse 或 NetBeans 是最常用的)。 As soon as you start to write anything at all large or complex, compiling and running from the command line without an IDE will cause you to claw your own eyes out.一旦您开始编写任何大型或复杂的东西,在没有 IDE 的情况下从命令行编译和运行会让您目瞪口呆。

When you write a class you can save the file as : MyClass.java and then execute this commands in this directory:编写类时,可以将文件另存为:MyClass.java,然后在此目录中执行以下命令:

javac MyClass.java

which will compile the class and then create automatically the file: MyClass.class (If compilation ended without errors)这将编译类,然后自动创建文件:MyClass.class(如果编译结束没有错误)

and then to run this SPECIFIC class execute the command:然后运行这个特定的类,执行命令:

java Myclass

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

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