Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a single Java file having main() in Eclipse, without creating an entire Java project?

Here's what I've got:

  1. I have a TestFile.java with the same class name.
  2. This class has a valid main() method.
  3. The file is easily run via terminal using javac and then java, gives intended output.

However, when I drag and drop(or open) the file into Eclipse and try to Run As, I don't get Run As Java Application or anything that can run that single/independent(of any project) Java file/class.

When I try to just Run, I get a pop-up saying "Run As Ant Build. Now, I never setup ant build.

All I want is to run just one this Java file and see the output/error in Console that is visible just below the editor in perspective.

I didn't and don't intend to write/setup a full fledged Java project with package name and all, in which case Run as Java Application simply comes. I just want to run one independent Java class/file in Eclipse with a main() method in it.

I am on Mac. If there's any other app/IDE which does that for Java and/or other languages then I think that will be very useful.

Update: I created a Java project in Eclipse and a test.java inside that and I keep changing/editing/adding in the same file whenever I need to write a quick code and run it. No way to really drag-drop-run in Eclipse.

like image 541
amar Avatar asked Jun 07 '13 02:06

amar


2 Answers

You can't compile & run just one file in Eclipse without the file being in a java project.

It is a very quick and easy process to create a Java Project in eclipse.

File -> New -> Java Project

You can use the default package (though it is not recommended) and put your single file in it and run it.

like image 70
neo108 Avatar answered Sep 22 '22 17:09

neo108


Try jGRASP, it's a really simple editor and compiler.

like image 27
Anubian Noob Avatar answered Sep 22 '22 17:09

Anubian Noob