dagblog.
← Archive

Getting started with Java: what you need and how to compile and run a program


The questions that come up most often in Java discussion groups
 are about the basics of working with the platform.

  • How do I run a Java program?
  • What do I need to run a Java program?
  • How do I compile a Java program?
  • How do I set the classpath?
  • How do I tell which class or file to run?

I'd like to quote a forum post that inspired me to put together this simple guide.
 I hope it helps newcomers. The passage below responds to one particular question,
 but it also answers the basic questions beginners tend to face.

I'm new to Java. I downloaded a package with several classes, but I don't know how to add it 
 to my system so the Java compiler will pick it up. I'm using Windows XP. The only instructions I've found 
 are for Windows ME. Apparently you're supposed to set it up somehow under System in the Control Panel.
 Does anyone know how? It would really help me out.
First, check whether your computer has the software it needs
to run Java applications, called the Java Runtime Environment
(JRE). It's available on its own, for example
from java.sun.com, and it also comes with the SDK.
I couldn't tell whether you have source files or files that are already compiled.
Source files have the extension java, while compiled files have the extension class
or come bundled as a single jar file. If you have
only java files, you'll need a compiler. The easiest way to get one
is through the SDK, which includes javac.
After installing the JRE or SDK, check that your operating system's Path
variable points to the directory
where Java is installed. Go to My Computer >> Properties >> Advanced >> Environment
Variables >> System variables >> Path. It should contain a path such as
č:\Program Files\Java\j2re1.4.0\bin\ or č:\Program
Files\Java\j2sdk1.4.2_02\bin. You can then compile and run your
programs from anywhere on your disk without having to type the full path
to the javac.exe compiler or the java.exe launcher.
Once you've set up your system this way, I recommend restarting it.
You can then go ahead and compile and run any Java program. For both compiling
and running programs, the classpath is especially important. It's an argument
that tells both the compiler and the launcher where to find libraries
outside the standard java.* packages.
There are essentially two ways to run compiled classes using the java.exe launcher.
  • You have a single jar file, which is the easier case
  • You have one or more class files
The first case is ideal. Usually you don't have to worry about the classpath or specify
which class to launch. Use java -jar nejakyprogram.jar
at a command prompt or in a console.
In the second case, you have to tell the launcher which file,
or rather which class, contains the main entry point. The source code has to contain
a method declared as public static void main(String args[]). The file containing
that method is the one to launch. Run java jmenosouboru, again
at a command prompt or in a console.
Here's the simplest possible example of compiling and running a program.
1.) Create a file named Foo.java (case matters) containing the following
public class Foo {
public static void main (String[] args){
System.out.println("Hellou world!");
}
}
2.) Compile Foo.java with the command javac Foo.java
3.) This creates a Foo.class file on disk. Run it with java Foo
If you're looking for more Java resources in Czech, I'd make sure to check out
vyuka.pecinovsky.cz and 
dione.zcu.cz/java

Medium version

Related articles

Related CZ Podcast episodes