HelloWorld is usually the first Java program a novice writes. It is a trivial program that just displays the string Hello World. This is not very impressive, but getting it to work proves you have your classpath, compiler and runtime all correctly installed and that you understand the basics of compling and running. This is a surprisingly difficult task. So break out the champagne when your little HelloWorld works.
Beginners start out writing simple programs that do not have a package like this:
// HelloWorld without a package public class HelloWorld { public static void main ( String [] args ) { out.println( "Hello World" ); } // end main } // end class HelloWorld
From the directory where HelloWorld.java lives, to compile it, type:
javac.exe HelloWorld.java
and to execute it:
rem execute java.exe -classpath . HelloWorld
If this does not work, here are some likely problem areas:
You may not specify the fully qualified drive and path: e.g.
rem do NOT do this java.exe -classpath . C:\TEMP\HelloWorld
is not permitted.
Did you remember to make your main method public and static? It must have a signature exactly like this:
public static void main ( String[] args ) { }
As soon as your projects need more than one class to solve, you will want to use packages. In this example, we will presume you own website mysite.com. To make your package globally unique, you choose to name the package for your HelloWorld as com.mysite.gettingstarted. HelloWorld.java needs to live in a directory called X:\com\mysite\gettingstarted\. mindprod.com code typically lives in packages with names like com.mindprod.xxxx. HelloWorld.class also needs to live in a directory called X:\com\mysite\gettingstarted\. The current directory needs to be X:\ for java.exe to be able to find the class. The name of the class you put on the java command line would be com.mysite.gettingstated.HelloWorld. The full truth is a little more complex, e.g. you can have zip and JAR files too.
// HelloWorld with a package package com.mysite.gettingstarted; public class HelloWorld { public static void main ( String [] args ) { out.println( "Hello World" ); } // end main } // end class HelloWorld
Set up the classpath:
rem set up classpath. Better done permanently in Control Panel. set classpath=X:\;.
From the directory where HelloWorld.java lives, to compile it, type:
X: cd \com\mysite\gettingstarted javac.exe HelloWorld.java
and to execute it
rem execute HelloWorld X: CD \ java.exe com.mysite.gettingstarted.HelloWorld rem -30-
If this does not work, here are some likely problem areas:
You may not specify the fully qualified drive and path: e.g.
java.exe -classpath . X:\com\mysite\gettingstarted\HelloWorld
is not permitted.
Did you remember to make your main method public and static? It must have a signature exactly like this:
public static void main ( String[] args ) { }
This page is posted |
http://mindprod.com/jgloss/helloworld.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\helloworld.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[18.226.166.106] |
| |
Feedback |
You are visitor number | |