Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile and run Eclipse Project from command prompt

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

like image 964
Abhishek Choudhary Avatar asked Sep 19 '13 19:09

Abhishek Choudhary


People also ask

How do I compile and run in command prompt?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How do I run Eclipse from command line?

If you need to launch Eclipse from the command line, you can use the symbolic link "eclipse" in the top-level eclipse folder. It refers to the eclipse executable inside the application bundle and takes the same arguments as "eclipse.exe" on other platforms.

How do I compile and run a program in Eclipse?

Step 1: Open Eclipse and click File > New > Java Project. Step 2: Provide the Project Name and click on the Finish button. Step 3: In the Package Explorer (left-hand side of the window) select the project which you have created. Step 4: Right-click on the src folder, select New > Class from the submenu.


2 Answers

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

like image 51
Jon Avatar answered Sep 23 '22 08:09

Jon


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.

like image 42
Ortwin Angermeier Avatar answered Sep 20 '22 08:09

Ortwin Angermeier