Compile and run Eclipse Project from command prompt

我们两清 提交于 2019-12-18 00:31:18

问题


How to compile and run Java Eclipse Project from command prompt?

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.

Is it possible?

Even with jar file, I found loading of static file was failing, as FileNotFoundException, how to solve that?

I meant to run like this-

http://www.skylit.com/javamethods/faqs/javaindos.html

First javac then java


回答1:


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.

Take a look at this answer: Eclipse: export running configuration for running the eclipse project from the console.




回答2:


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)

  1. Run your project into Eclipse
  2. Goto Debug perspective
  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
  5. Click on the 'Command Line' field (this is what you probably want).
  6. Press [ctrl]+A & [ctrl]+C to select and copy
  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.

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. 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).




回答3:


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

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:

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:

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

For more information refer below links:

Compiling With javac - javapractices

Build Eclipse Java Project from Command Line - Stackoverflow




回答4:


Select the project and click on File->Export which opens a new window.

From that select Runnablejar option and click next button.

In launch configuration select your main class and in export destination give the path where you want to store the jar file.



来源:https://stackoverflow.com/questions/18902934/compile-and-run-eclipse-project-from-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!