简体   繁体   English

从命令提示符编译并运行Eclipse Project

[英]Compile and run Eclipse Project from command prompt

How to compile and run Java Eclipse Project from command prompt? 如何从命令提示符编译和运行Java Eclipse Project?

How to run a Java Eclipse project from Command Line with java file name only. 如何仅使用java文件名从命令行运行Java Eclipse项目。 I don't want to to use class file or jar files generated by 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

For building you can export an Ant build file. 对于构建,您可以导出Ant构建文件。 Just right click on the project -> Export -> Ant buildfiles . 只需右键单击项目 - > 导出 - > Ant构建文件 On the command promt use ant <buildfile> to build the project. 在命令promt上使用ant <buildfile> buildfile ant <buildfile>来构建项目。

Take a look at this answer: Eclipse: export running configuration for running the eclipse project from the console. 看一下这个答案: Eclipse:从控制台导出运行 eclipse项目的导出运行配置

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. 这将编译到位,并在.java文件旁边创建.class文件。 If you want to place generated class files elsewhere, use the -d option to put them into an existing directory: 如果要将生成的类文件放在其他位置,请使用-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: 如果你的jar在各种目录中,那么classpath是一个由分号分隔的列表:

-cp lib\*;C:\abc\one.jar;C:\xyz\two.jar

For more information refer below links: 有关更多信息,请参阅以下链接

Compiling With javac - javapractices 用javac编译 - javapractices

Build Eclipse Java Project from Command Line - Stackoverflow 从命令行构建Eclipse Java项目 - Stackoverflow

Kind of old question I know, but if you want to know command prompt for running Eclipse-based project (ie the one that Eclipse uses) 我知道一些旧问题,但是如果你想知道运行基于Eclipse的项目的命令提示符(即Eclipse使用的那个)

  1. Run your project into Eclipse 将项目运行到Eclipse中
  2. Goto Debug perspective 转到Debug透视图
  3. (on my screen anyway) Window in top left corner should have a little 'debug' tab. (无论如何在我的屏幕上)左上角的窗口应该有一个小的“调试”选项卡。
  4. Right click on name of your project, select Properties at the bottom of drop-down 右键单击项目名称,选择下拉列表底部的“ Properties
  5. Click on the 'Command Line' field (this is what you probably want). 单击“命令行”字段(这是您可能想要的)。
  6. Press [ctrl]+A & [ctrl]+C to select and copy 按[ctrl] + A和[ctrl] + C进行选择和复制
  7. Either paste this into command line, or 将其粘贴到命令行中,或者
  8. (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. (我做了什么)在Windows中,创建新的*.bat文本文件并将其粘贴到那里...现在双击该文件应该运行你的java项目。

Pretty useful if you wanna run something out of eclipse and are lazy like me. 如果你想从日食中运行并像我一样懒惰,那就非常有用。

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. 当然,他们也希望看到*.java文件中的代码,而上面只使用*.class文件Eclipse即时构建。 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. 要批量编译* .java文件然后运行,您需要在上面的进程中获得的javaw行之前放置一个适当的javac命令并相应地进行调整 - 请查看Java Docs。 Eclipse has done most of hard work with library class paths though (I was using a few libs). 虽然Eclipse已经使用库类路径完成了大部分工作(我使用了一些库)。

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文件的路径。

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

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