amar
amar

Reputation: 475

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.

Upvotes: 14

Views: 35125

Answers (4)

Paul Verest
Paul Verest

Reputation: 63912

You can do that in any Eclipse project with org.eclipse.jdt.core.javanature (check/edit .project file).

So just open .project file and add

<natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
</natures>

if it is not yet there.

Upvotes: 0

Anubian Noob
Anubian Noob

Reputation: 13596

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

Upvotes: 3

neo108
neo108

Reputation: 5246

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.

Upvotes: 10

cheng6588
cheng6588

Reputation: 1

You can run the java file in the cmd command window.

make sure you have install the java environment.

java -version

output: java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b11) Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

cd javafilepath

java yourjavafilename.java

Upvotes: -2

Related Questions