Java 如何在命令提示符下运行 NetBeans 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/626940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a NetBeans project in command prompt?
提问by
My professor asked us to create a Java program that would be able to run in command prompt but could also be opened using NetBeans.
我的教授要求我们创建一个 Java 程序,它可以在命令提示符下运行,但也可以使用 NetBeans 打开。
The program is about using the different types of sorting (specifically Selection, Insertion, Exchange, Quick, and Heap sorting). our professor specifically told us to use object oriented programming in Java, and that she wants to see a main class plus the different classes that would do the sorting.
该程序是关于使用不同类型的排序(特别是选择、插入、交换、快速和堆排序)。我们的教授特别告诉我们在 Java 中使用面向对象的编程,并且她希望看到一个主类加上将进行排序的不同类。
I tried to write the program in NetBeans — thinking that later I could simply run the program in cmd using javac.
我尝试在 NetBeans 中编写程序 - 认为以后我可以使用 javac 在 cmd 中简单地运行该程序。
In cmd, I typed the path where my NetBeans project was saved and I tried to compile the files using javac. but it says that "'javac' is not recognized as an internal or external command, operable program or batch file."
在 cmd 中,我输入了保存 NetBeans 项目的路径,并尝试使用 javac 编译文件。但它说“'javac'不是内部或外部命令,也不是可运行的程序或批处理文件。”
So I tried to save the files in sun>sdk>jdk>bin, and from there I tried to compile the files, and it was fine. The problem sets in when I tried to run them.
所以我尝试将文件保存在 sun>sdk>jdk>bin 中,然后我尝试编译这些文件,结果很好。当我试图运行它们时,问题就出现了。
Here's how I tried to compile the files:
这是我尝试编译文件的方式:
javac Main.java
Sortchoice.java
Selection.java
SelectionSort.java
Insertion.java
InsertionSort.java
Exchange.java
ExchangeSort.java
(I havent finished the syntax for the next two sorting.)
(我还没有完成接下来两个排序的语法。)
Here's how I tried to run the files in cmd:
这是我尝试在 cmd 中运行文件的方法:
java Main Sortchoice Selection SelectionSort Insertion InsertionSort Exchange ExchangeSort
and cmd says:
和 cmd 说:
exception in thread "main" java.lang.NoClassDefFoundError: main (wring name: myjava/Main)
at java.lang.ClassLoader.defineClass1(Nativ... Method)"
at java.lang.ClassLoader.defineClass(ClassL...
at java.security.SecureClassLoader.defineCl...
at java.net.URLClassLoader.defineClass(URLC...
at java.net.URLClassLoader.access##代码##0(URLCl...
at java.net.URLClassLoader.run(URLClassLo...
at java.security.AccessController.doPrivile... Method)
at java.net.URLClassLoader.findClass(URLCla...
at java.lang.ClassLoader.loadClass(ClassLoa...
at sun.misc.Launcher&AppClassLoader.loadCla...
at java.lang.ClasLoader.loadClass(ClassLoad...
at java.lang.ClassLoader.loadClassInternal(...
What should I do? (Sorry for my kilometric-long explanation. I just wanted to put in as many details as possible.)
我该怎么办?(对不起,我一公里长的解释。我只是想输入尽可能多的细节。)
I would also like to emphasize that I'm just a beginner in Java programming.
我还要强调,我只是 Java 编程的初学者。
回答by Michael Myers
- It would be better to add javac to your PATH environment variable instead of putting the .java files into the same directory with it. It'll get awfully crowded in there.
To run, you just need
java Main
instead of putting every class on the command line.
Did you declare a package in your .java files? Like,
package myjava;
? If so, the command string must be
java myjava.Main
- 最好将 javac 添加到您的 PATH 环境变量中,而不是将 .java 文件与它放在同一目录中。里面会人满为患。
要运行,您只需要
java Main
而不是将每个类都放在命令行上。
您是否在 .java 文件中声明了一个包?喜欢,
包 myjava;
? 如果是这样,命令字符串必须是
java myjava.Main
Does that answer your questions?
这能回答你的问题吗?
回答by Jared
Follow these steps (code examples are just examples, you will need to tweak some for your setup):
请按照以下步骤操作(代码示例只是示例,您需要针对您的设置进行一些调整):
Set your JAVA_HOME to the directory where the JDK is installed.
set JAVA_HOME="c:\Program Files\Java\jre1.6.0_12"
Set your PATH to include the
bin
directory in JAVA_HOME.set PATH=%PATH%:%JAVA_HOME%\bin
Change to the root directory of your source code. If you have declared your code to be in packages, this is the root directory of your package structure. If not, this is the directory that contains your .java files:
cd c:\My\Source\Directory
Execute javac with your Java files as the argument:
javac Class1.java Class2.java
(Assuming everything compiles correctly) Execute
java
with the name of the class containing your main method as the argument (if you included package declarations, then your class name is fully-qualified, meaning it should include the package name before the class name, with a '.' separating the package name from the class name):java Main
将您的 JAVA_HOME 设置为安装 JDK 的目录。
设置 JAVA_HOME="c:\Program Files\Java\jre1.6.0_12"
设置 PATH 以包含
bin
JAVA_HOME 中的目录。设置 PATH=%PATH%:%JAVA_HOME%\bin
切换到源代码的根目录。如果您已将代码声明在包中,则这是包结构的根目录。如果没有,这是包含 .java 文件的目录:
cd c:\My\Source\Directory
使用您的 Java 文件作为参数执行 javac:
javac Class1.java Class2.java
(假设一切都正确编译)
java
以包含 main 方法的类的名称作为参数执行(如果包含包声明,则类名是完全限定的,这意味着它应该在类名之前包含包名,并带有'.' 将包名与类名分开):主程序
回答by TofuBeer
The best way to do it is this:
最好的方法是这样的:
1) create a directory with src\
and tests\
in it (the tests is optional if you are not using JUnit).
1)创建一个目录src\
,并tests\
在它(测试,如果你没有使用JUnit是可选)。
2) assuming you have package myjava;
at the top of your files (and make sure that this is what your prof wants, it becomes a pain to mark things if they are not in the right place), make a src\myjava
directory (and if you are doing JUnit a tests\myjava
directory).
2)假设你package myjava;
在你的文件的顶部(并确保这是你的教授想要的,如果它们不在正确的位置,标记东西会变得很痛苦),创建一个src\myjava
目录(如果你正在做 JUnit一个tests\myjava
目录)。
3) copy your files into the src\myjava
directory
3) 将你的文件复制到src\myjava
目录中
4) delete your NetBeans project and recreate it as a new on with exising sources. When you are setting up the src
(and optional test
) directories add the src\
(and optionally the tests\
) directory. DO NOT add the src\myjava
directory or it won't work in NetBeans.
4) 删除您的 NetBeans 项目并使用现有源重新创建它。当您设置src
(和可选test
)目录时,添加src\
(和可选tests\
)目录。不要添加src\myjava
目录,否则它在 NetBeans 中不起作用。
5) make a directory called classes\
(so you you have src\
, classes\
, and maybe \tests
all in the same place)
5) 创建一个名为的目录classes\
(所以你有src\
, classes\
,也许\tests
都在同一个地方)
6) on the command line type javac -d classes -cp classes src/myjava/*.java
6)在命令行输入 javac -d classes -cp classes src/myjava/*.java
-d
tells the compiler where to put the .class files-cp
tells the compiler where to look for classfilessrc/myjava/*.java
tells it to compile all of the .java files insrc/myjava
-d
告诉编译器将 .class 文件放在哪里-cp
告诉编译器在哪里寻找类文件src/myjava/*.java
告诉它编译所有的 .java 文件src/myjava
7) run it via java -cp classes myjava.Main
7)通过运行它 java -cp classes myjava.Main
-cp
classes tells it to look in the classes directory for the .class filesmyjava.Main
is the name of the class to run
-cp
classes 告诉它在 classes 目录中查找 .class 文件myjava.Main
是要运行的类的名称
回答by Busta
Right click My Computer>properties>advanced>environment variables...add the bin directory in jdk to the Path variable example: Variable Name: path Variable value: C:\Program Files\Java\jdk1.6.0_16\bin
右键我的电脑>属性>高级>环境变量...将jdk中的bin目录添加到Path变量示例:变量名:path变量值:C:\Program Files\Java\jdk1.6.0_16\bin
java and javac should now work from any directory in the command prompt
java 和 javac 现在应该可以在命令提示符的任何目录下工作
回答by Cricket
If you click build inside NetBeans, it should give you (in your compiler output) a message like, "To run this application from the command line without Ant, try: java -jar yourPathToRun"
如果您在 NetBeans 中单击构建,它应该给您(在您的编译器输出中)一条消息,例如“要在没有 Ant 的情况下从命令行运行此应用程序,请尝试:java -jar yourPathToRun”
回答by Omar
Create a folder somewhere, say C:\myjava
. Copy your .java
source files from wherever netbeans saves them to your C:\java
for example. Open each of these .java
files you have just pasted and delete the line that states the package e.g package myjava
. Compile using javac
e.g. javac c:\myjava\MyJava.java
. Then run it as in, java c:\myjava\MyJava
在某处创建一个文件夹,比如C:\myjava
. 例如,.java
从 netbeans 将它们保存到的任何位置复制源文件C:\java
。打开.java
您刚刚粘贴的每个文件并删除声明包的行,例如package myjava
. 使用javac
例如编译javac c:\myjava\MyJava.java
。然后像java一样运行它c:\myjava\MyJava
回答by Michael Swartz
The easiest way to do this is to run the .jar
file in the dist
folder under your project's main folder. Make sure the path to the jdk bin
folder in your computer's System
, Environmental Variables
is set to find the usual: jar.exe
, java.exe
, javac.exe
, etc, (see other posts in this thread if you need instructions on doing that).
要做到这一点最简单的方法是运行.jar
该文件dist
夹下的项目的主文件夹中。确保路径JDKbin
文件夹中的计算机System
,Environmental Variables
设定要发现通常的:jar.exe
,java.exe
,javac.exe
,等(见在此线程的其他职位,如果你需要做的是说明)。
1) In Netbeans do a clean build, press F11
or click on the Run
menu and click on Build Project
. Click through to get past all the prompts, you need a clean build to do this.
1) 在 Netbeans 中进行干净构建,按下F11
或单击Run
菜单并单击Build Project
。单击以跳过所有提示,您需要一个干净的构建来执行此操作。
2) in the command prompt navigate to your project's dist
folder
2)在命令提示符中导航到您的项目dist
文件夹
3) on the command line type in and run: java -jar yourMainFile.jar
3)在命令行输入并运行: java -jar yourMainFile.jar
No creating separate folders, no copying files, no multiple files to include on the command line argument. But you will have to re-build your project each time you change your code before running it on the command line.
无需创建单独的文件夹,无需复制文件,无需在命令行参数中包含多个文件。但是,每次在命令行上运行代码之前,您都必须重新构建项目。