14

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
  • 1
    A `.java` file is not executable. You can't use it to launch a Java program from the command line. – Sotirios Delimanolis Sep 19 '13 at 19:08
  • updated the expected way to execute – Abhishek Choudhary Sep 19 '13 at 19:12
  • *"I found loading of static file was failing"* WDYM by 'static' file? Is this resource supplied by you or the user? If by you, access it as an [tag:embedded-resource] by `URL`. If the user, offer them a [`JFileChooser`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html). – Andrew Thompson Sep 19 '13 at 19:18

4 Answers4

14

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

Jon
  • 2,280
  • 2
  • 25
  • 33
  • Worked very well for me. I had to remove some parameters on the front, but the classpath with all the libraries was correct and therefore a big help. – MrSmith42 May 26 '16 at 07:36
  • These steps are no longer valid in 2022. :< There is no debug perspective. While there is a right-click-Preferences option from the Package explorer, there is no 'Command Line' field in the resulting dialog box. – Peter Leopold Nov 15 '22 at 19:16
11

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.

Community
  • 1
  • 1
Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34
  • The link is to a page that has nothing to do with the problem building/running an eclipse project from the command line. If you follow the advice there, you get an xml file, and nothing on how to configure javac or java. – Peter Leopold Nov 15 '22 at 19:39
11

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

Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33
2

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.