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

孤人 提交于 2019-12-04 16:59:52

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/16975422/how-do-i-run-a-single-java-file-having-main-in-eclipse-without-creating-an-en

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