Create a Java program

The following description is just a short guide for beginners that explains, which programs are needed for the Java programming and how to create a program and execute it in principle.

Nowadays, Java is everywhere: on the PC, mobile, navigation,...
The nice thing on Java is, that it is platform independent, so you only have to install the correct runtime environment.

Here I just deal with Java applications via command line.

But for the first steps, this is the best anyway.

I am going to explain in steps, how to create a program and what you all need to, but I will not comment explicitly on the programming itself.

Step 1:
To program Java, you must first download the so-called "Java Software Development Kit (JDK)".
Therefore it is not enough to install the normal "Java Runtimes" (The size of the "Java Software Development Kit (JDK)" is much larger).

Step 2:
When it is downloaded, then you must install it in any path.

Step 3:
Create a file folder with the name "Java" somewhere on your PC.

Step 4:
Create a batch file for compiling the Java programs:
Open the editor and write the following in it:
1
2
"C:\Program Files (x86)\Java\jdk1.5.0_04\bin\javac" Testprogram.java
pause


Of course, you must adapt the text, depending on which path you have installed the "Java Software Development Kit (JDK)" and what version it is.
The "Testprogram.java" is the name of the subsequent program file and of course you can choose another name.

This can be saved as a batch file (*.bat) (no *.txt at the end of the file!).
Now, the name of the file is for example "Compiler.bat"

Step 5:
Now we create another batch file to run the program.
So you must open the editor again and write the following into it:
1
2
"C:\Program Files (x86)\Java\jdk1.5.0_04\bin\java" Testprogram
pause


Then save it for example as "Start.bat".

Step 6:
Now we can create our first program.
So we open the editor again and write into it the program.
For example the following program (addition of two numbers):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;
import java.util.Scanner;
class Testprogram {

public static void main(String[] args) {

	long eingabe1, eingabe2, ergebnis;
	System.out.println("Please enter the two numbers, which should be added!");
	Scanner eingabe = new Scanner(System.in);
	eingabe1 = Long.valueOf(eingabe.next());
	eingabe2 = Long.valueOf(eingabe.next());
	ergebnis = eingabe1 + eingabe2;
	System.out.println(eingabe1 + " + " + eingabe2 + " = " + ergebnis);
	
}


Then save it with the file extension *.java (for example "Testprogram.java").

Step 7:
Compile the program: just click on "Compiler.bat" → the command line opens.
If the program includes no errors, then a new file called "Testprogram.class" is created. If there are errors in the code, then an error message is displayed on the command line.

Step 8:
Run program: click on "Start.bat" → command line opens and starts the program.

Ready
Share this page
Advertising
Posted on 17.10.2011 | Last modified on 20.11.2016