Vous êtes sur la page 1sur 2

27.11.

2013

How to Compile and Run Java Code from a Command Line

Random Programming Notes from a Web Developer


by Sergiy Kovalchuk

How to Compile and Run Java Code from a Command


Line
Java

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 ";").

Compiling Java Code


The first step is compiling plain text .javasources into Java Virtual Machine byte code
(.classfiles). This is done with javac utility that comes with JDK.
Assuming we are at the application root folder trying to compile Application.java file
from com.example package that uses lib1.jar and lib2.jar libraries from lib folder to a
destination binfolder, compilation command should have the following format:

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 Open Source Projects


HT ML Com pr essor a n d Min ifier
jQu er y Loa dm a sk Plu g in
Ch r om e T ex t a r ea For m a t t er

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...

As a result bin/com/example/Application.class file should be created. If


Application.javauses other classes from the project, they all should be automatically compiled

StackOv erflow Profile

and put into corresponding folders.

Running Java Code


To launch a .classfile we just compiled, another JDK utility called java would be needed.
Assuming we are at the application root folder trying to launch Application.class file
from com.example package that uses lib1.jar and lib2.jar libraries from lib folder, the
launch command should have the following format
java -cp bin;lib/lib1.jar;lib/lib2.jar com.example.Application

Note that we don't provide a filename here, only an actual class name that java would
attempt to find based on provided classpath.

Some Notes About Classpath


Lets say during Application.java compilation a compiler stumbles upon some
com.example.Utilclass. How to find it in the file system? According to Java file naming rules,
Utilclass has to be located somewhere in Util.javafile under /com/example/folder, but where
to start searching for this path? Here is where classpath comes into play which sets the starting
folder for searching for classes. Classpath can be set in 3 different ways:
If no --classpathparameter is passed, CLASSPATHenvironment variable is used
If CLASSPATHenvironment variable is not found, current folder (".") is used by default
If --classpathis explicitly set as a command line parameter, it overrides all other values
The fact that classpath when set overrides default value (current folder) can cause some
unexpected results.
For example if we don't use any third party libraries, only our own com.example.Util
class, and try to compile Application.javafrom the srcfolder:
www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/

1/2

27.11.2013

How to Compile and Run Java Code from a Command Line

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

it would cause an error:


package com.example.Util does not exist

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

2011 S ergiy Kov alchuk | C ontact

0.03216

www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/

2/2

Vous aimerez peut-être aussi