Josh P
Josh P

Reputation: 29

Program to open .java files and run as if it were in Eclipse's console

I need a simple program that I can open and run .java files with on my Mac as if it were Eclipse's console to where the program will run, Is there something simpler than programs such as Eclipse?

Upvotes: 1

Views: 55

Answers (2)

Matt
Matt

Reputation: 3760

As the commenters have alluded to, you don't need much at all. You need a text editor (TextEdit would work) and a Terminal so you can invoke the following commands to compile and run your program:

javac myProgram.java
java myProgram

See the Oracle tutorial for a little more information

This is for the very simple case, however, refer to @laune's answer. As soon as your program is more complicated than the simplest case, you will need to start thinking about your classpath etc.

Upvotes: 1

laune
laune

Reputation: 31290

One does not simply "run" Java files. You might have noticed that Eclipse compiles the Java text into byte code on .class files. Then, you need to define to Eclipse that a certain class contains public static void main(String[] args) so it can be started as a main program. Implicitly other .class files may be loaded due to imports from the main program, and so on.

You can call the Java compiler from an application you could write - see javax.tools with interface JavaCompiler and class ToolProvider. You may also have to use java.lang.Process and ProcessBuilder so you can run the resulting .class as a subprocess.

In its simplest form (single .java, no imports) it isn't much more than, say, 50 lines of Java code. But as soon as you need some additional features it'll grow quickly.

Much of what I've described can also be achieved by writing a shell script, i.e., you can call the Java compiler and run the program using only a handful of script lines - no need to write an application. Here again, defining a classpath (if there are imports), passing in parameters to the program, etc. need extra features - so typically it's not just the two lines in that other answer.

Upvotes: 2

Related Questions