LKBEN11081: Howto write a litte "hello world" program with java
You want to write a litte program in java on linux or windows
For writing a small program, you can use just any text editor. (e.g. notepad under Windows or vim under Linux)
In Java, everything is a class, so we will need to write a class to start with.
public static void main(String args)
String MyMessage = "Am I your first Java program?";
You need to save this file with the name MyClass.java otherwise it will not function. This is a convention in java. This file is called your java source code. It cannot be run like this, it has to be compiled first. This can be done on the command line. (under windows cmd.exe, for linux your shell e.g. bash)
Please note the writing of the MyClass. It is case sensitive, to check you can check it with dir on windows or ls -la on linux. Also note that the command javac is not present on every computer system which has the ability to run java. There are mainly two versions of java. A runtime and a development. To write programs you need the java development kit version.
After succesfully running the command you will notice a new file in the directory named "MyClass.class". This is a file that can be run with the java command and is compiled into java bytecode. This is not directly executable on the cpu and still has to be "just in time compiled" for the machine. To run this program you need to give it as parameter to the java runtime environment.
Please note that the command "java MyClass.class" will deliver an "Exception in thread main". This is by design, you should not add the class extentions to the java runtime environment. You should also stand in the directory where the program is located. e.g. Running java c:\temp\MyClass will also deliver an "Exception in thread main" unless you define the parameter classpath or your command line stand in c:\temp.
java -classpath c:\temp MyClass
When everything runs fine, you will see the output of the program.
Am I your first Java program?
As you noticed, there are quite a few pitfalls but once you know them, it is easy to start writing programs in java.
Disclaimer:The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!
About the Author
Author:- Keskon GmbH & Co. KG
Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.