Académique Documents
Professionnel Documents
Culture Documents
2013
Being spoiled by IDEs and automated building tools I recently realized that I don't know
how to run java code from a command line anymore. After playing a guessing game for an hour
trying to compile a simple piece of code that took 5 minutes to write, I thought maybe it's time
to do a little research.
Task
Lets say we have a fairly standard java project that consists of three top level folders:
/bin - empty folder that will contain compiled .classfiles
/lib - contains third party .jarfiles
/src - contains .javasource files
Our task would be to compile and launch the project from its root folder. We will use
Windows OS as example (on Unix systems the only difference would be path separation symbol
- ":"instead of ";").
Home
Latest Articles
How t o Im plem en t Fa cebook A pp
A u t h or iza t ion in Ja v a
How t o Com pile a n d Ru n Ja v a Code
fr om a Com m a n d Lin e
How t o Post on A pp W a ll a s A dm in
fr om Fa cebook A PI
How t o Ma k e Ja v a MD5 Ma t ch PHP
a n d My SQL MD5
Cr ea t in g Cu st om Dir ect iv es for
A pa ch e V elocit y
Cr ea t in g Cu st om T ools for A pa ch e
V elocit y
How t o For ce Bu ild W it h FIX ME
Block s t o Fa il in A n t
How t o In t eg r a t e V elocit y T ools 2 .0
w it h Spr in g
How
t o W r it e
Ma n y -T o-Ma n y
Sea r ch Qu er ies in My SQL a n d
Hiber n a t e
Gu ide t o Select in g A ppr opr ia t e
Ma p/Collect ion in Ja v a
How t o It er a t e Ov er a Ma p in Ja v a
My Chrom e Ex tensions
W ebSt or e In spect or
jQu er y A pi Br ow ser
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/Application
.java
V iew Ba ck g r ou n d Im a g e
Ch r om e Upda t e Not ifier
a n d a bu n ch of ot h er s...
Note that we don't provide a filename here, only an actual class name that java would
attempt to find based on provided classpath.
1/2
27.11.2013
javac com/example/Application.java
this would work, but then if we decide to add a third party libarary to the classpath:
javac -cp lib/lib1.jar com/example/Application.java
This happens because when we set -cp lib/lib1.jarwe override default value for the
classpath - current folder. Now a compiler will be looking for all classes only inside that jar file.
To fix this we need to explicitly add the current folder to the classpath:
javac -cp .;lib/lib1.jar com/example/Application.java
Jul 9, 2011
0.03216
www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/
2/2