Tuesday, July 28, 2009

How do I compile a program in java in a terminal (linux)?

I am working on a university machine through ssh. I know they have a java compiler installed. What is the the equilvance in java to g++ foo.cpp -o foo ???





Thanks!

How do I compile a program in java in a terminal (linux)?
Compilation is done with the Java compiler, javac. To compile a single file:





javac foo.java





This will create foo.class, and maybe additional class files if you use some advanced Java features (specifically inner classes).





Java .class files are more or less the equivalent of object files, though they're Java bytecode rather than the machine's native code.





To compile multiple files, just list them on the command line as well:





javac foo.java bar.java


or


javac *.java





There's no true Java equivalent to "-o foo", as Java doesn't make executable programs from the OS point of view. You can package your classes up into an "application archive" that the Java virtual machine will execute, though.





Once you have all your .class files created, you package them in a jar (Java ARchive) with the "jar" command, which takes the same arguments (more or less) as the Unix "tar" command:





jar cvf myapp.jar *.class





Note that the class package naming and directory structure must match. If you place your classes in the package "foo.bar", you'll want to create the .jar so that it archives a directory "foo", which contains a directory "bar", which contains your classes.





If you need to see the common command-line options for the Java compiler, see the documentation for the appropriate version (execute "java -version" to see which it is, then go to java.sun.com), or execute "javac -help" for a brief overview.
Reply:% javac -g Foo.java





Links below is how to compile java programs and the javac (Java compiler) options.


No comments:

Post a Comment