Some 'javac' and 'java' options

Normally we use plain javac to compile all java files.

In this I will try to elicit some of the useful options associated with javac.
A normal compilation puts the class files in the same directory as the java files.

project1
|
|--------src
|        |
|        |----Class1.java
|
|--------classes

This seems to be a better structure for the same.
bash$pwd
/project1
bash$javac -d classes src/Class1.java
This will put the class file in the classes directory.
Also, 1 important point is that this command will take care of the package structure automatically.
The last thing about this is that if the classes directory didn't exist we will get a compiler error.
src/Class1.java:1: error while writing Class1: classes/Class1.class (No such file or directory)
public class Class1{
\^
1 error

Running with java
bash$java -cp classes Class1
Hello World

We are all familiar with this run.

Consider this.
bash$java -Dmyprop=myValue -cp classes Class1
Hello World

It still gives the same output. What's the difference?
To state in 1 line -D switch creates a system property with the name myprop and assigns the value myValue to it.
This is useful in running a single app over multiple environments with a little customized start up script.
In case you don't know system properties.
Run this
bash-3.00$ cat src/TestProps.java
import java.util.\*;
public class TestProps {
public static void main(String[] args) {
Properties p = System.getProperties();
p.list(System.out);
}
}

bash-3.00$ java -cp classes/ TestProps
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=/usr/jdk/instances/jdk1.5.0/jre/lib/s...
java.vm.version=1.5.0_14-b03
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=:
java.vm.name=Java HotSpot(TM) Server VM
file.encoding.pkg=sun.io
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/net/jpsesvr/space2/jpse/vikram/files...
java.runtime.version=1.5.0_14-b03
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
java.endorsed.dirs=/usr/jdk/instances/jdk1.5.0/jre/lib/e...
os.arch=sparc
java.io.tmpdir=/var/tmp/
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
os.name=SunOS
myProp=myValue
sun.jnu.encoding=ISO646-US
java.library.path=/usr/jdk/instances/jdk1.5.0/jre/lib/s...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Server Compiler
os.version=5.10
user.home=/home/va203678
user.timezone=
java.awt.printerjob=sun.print.PSPrinterJob
file.encoding=ISO646-US
java.specification.version=1.5
user.name=va203678
java.class.path=classes/
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=/usr/jdk/instances/jdk1.5.0/jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
java.vm.info=mixed mode
java.version=1.5.0_14
java.ext.dirs=/usr/jdk/instances/jdk1.5.0/jre/lib/ext
sun.boot.class.path=/usr/jdk/instances/jdk1.5.0/jre/lib/r...
java.vendor=Sun Microsystems Inc.
file.separator=/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=big
sun.io.unicode.encoding=UnicodeBig
sun.desktop=gnome
sun.cpu.isalist=sparcv9 sparcv8plus sparcv8 sparcv8-f...

Comments:

Nice one ! You have any doc. where I can get all java and javac options ?

Posted by Vaibhav on April 25, 2008 at 01:32 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

I am Vikram Aroskar working in the Java team based out of India.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today