Java 从命令提示符编译并运行 Eclipse 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18902934/
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
Compile and run Eclipse Project from command prompt
提问by Abhishek Choudhary
How to compile and run Java Eclipse Project from command prompt?
如何从命令提示符编译和运行 Java Eclipse 项目?
How to run a Java Eclipse project from Command Line with java file name only. I don't want to to use class file or jar files generated by Eclipse.
如何从命令行仅使用 java 文件名运行 Java Eclipse 项目。我不想使用 Eclipse 生成的类文件或 jar 文件。
Is it possible?
是否可以?
Even with jar file, I found loading of static file was failing, as FileNotFoundException
, how to solve that?
即使使用 jar 文件,我也发现加载静态文件失败,因为FileNotFoundException
,如何解决?
I meant to run like this-
我是想这样跑的——
http://www.skylit.com/javamethods/faqs/javaindos.html
http://www.skylit.com/javamethods/faqs/javaindos.html
First javac
then java
先javac
然后java
回答by Ortwin Angermeier
For building you can export an Ant build file. Just right click on the project -> Export-> Ant buildfiles. On the command promt use ant <buildfile>
to build the project.
对于构建,您可以导出 Ant 构建文件。只需右键单击项目 ->导出-> Ant buildfiles。在命令 promt 上用于ant <buildfile>
构建项目。
Take a look at this answer: Eclipse: export running configurationfor running the eclipse project from the console.
看看这个答案:Eclipse: export running configurationfor running the eclipse project from the console。
回答by springhibernatetutes
Select the project and click on File->Export which opens a new window.
选择项目并单击 File->Export 这将打开一个新窗口。
From that select Runnablejar option and click next button.
从中选择 Runnablejar 选项并单击下一步按钮。
In launch configuration select your main class and in export destination give the path where you want to store the jar file.
在启动配置中选择您的主类并在导出目标中提供您要存储 jar 文件的路径。
回答by Jon
Kind of old question I know, but if you want to know command prompt for running Eclipse-based project (i.e the one that Eclipse uses)
我知道一个老问题,但是如果您想知道运行基于 Eclipse 的项目的命令提示符(即 Eclipse 使用的项目)
- Run your project into Eclipse
- Goto Debug perspective
- (on my screen anyway) Window in top left corner should have a little 'debug' tab.
- Right click on name of your project, select
Properties
at the bottom of drop-down - Click on the 'Command Line' field (this is what you probably want).
- Press [ctrl]+A & [ctrl]+C to select and copy
- Either paste this into command line, or
- (what I did) in Windows, create new
*.bat
text file and paste it in there ... now double clicking on that file should run your java proj.
- 在 Eclipse 中运行你的项目
- 转到调试透视图
- (无论如何在我的屏幕上)左上角的窗口应该有一个小“调试”选项卡。
- 右键单击您的项目名称,
Properties
在下拉列表底部选择 - 单击“命令行”字段(这可能是您想要的)。
- 按 [ctrl]+A & [ctrl]+C 选择并复制
- 要么将其粘贴到命令行中,要么
- (我所做的)在 Windows 中,创建新的
*.bat
文本文件并将其粘贴在那里......现在双击该文件应该运行你的 java 项目。
Pretty useful if you wanna run something out of eclipse and are lazy like me.
如果你想从 eclipse 中运行一些东西并且像我一样懒惰,这非常有用。
BTW I needed this for exporting project for a uni assignment. Of course, they wanted to see code in *.java
files too and above just uses *.class
files Eclipse builds on the fly.
To make batch compile your *.java files & then run you need to put together an appropriate javac
command before the javaw
line you got from process above and adjust accordingly - look at Java Docs for this. Eclipse has done most of hard work with library class paths though (I was using a few libs).
顺便说一句,我需要这个来导出一个 uni 作业的项目。当然,他们也希望看到*.java
文件中的代码,并且上面只使用*.class
Eclipse 动态构建的文件。要批量编译您的 *.java 文件然后运行,您需要在从上面的过程中获得javac
的javaw
行之前组合一个适当的命令并进行相应的 调整 - 请查看 Java 文档。尽管 Eclipse 已经完成了库类路径的大部分艰苦工作(我使用了一些库)。
回答by OO7
Assumes a project with the following directory structure:
假设一个项目具有以下目录结构:
PROJECT_HOME
-> lib (jar files)
-> src (java code)
-> hirondelle (top-level package; no .java files)
-> ante (.java files)
-> deluvian (.java files)
Compiling With javac
用 javac 编译
PROJECT_HOME>javac -cp lib\* src\hirondelle\ante\*.java src\hirondelle\ante\deluvian\*.java
This compiles in place, and creates .class files beside .java files. If you want to place generated class files elsewhere, use the -d option to put them into an existing directory:
这将编译到位,并在 .java 文件旁边创建 .class 文件。如果要将生成的类文件放在其他地方,请使用 -d 选项将它们放入现有目录:
PROJECT_HOME>javac -cp lib\* -d build src\hirondelle\ante\*.java src\hirondelle\ante\deluvian\*.java
If your jars are in various directories, then the classpath is a list delimited by semi-colons:
如果您的 jars 在不同的目录中,那么类路径是一个由分号分隔的列表:
-cp lib\*;C:\abc\one.jar;C:\xyz\two.jar
For more informationrefer below links:
有关更多信息,请参阅以下链接:
Compiling With javac - javapractices
Build Eclipse Java Project from Command Line - Stackoverflow