Running Java Applications
Note that Java is case sensitive.
- Create your source code, and store it in a file with a .java extension, e.g. YourProgram.java.
- Run the compiler (javac (in lower case) in the case of the JDK):
javac YourProgram.java
- The compiler creates a bytecode program called YourProgram.class, if there are no errors.
- Execute YourProgram.class:
java YourProgram
- If you have compiled an application, it should now run on any Java virtual machine on any machine that has it.
- If you have compiled an applet, you need to embed it in a web page first, then it can execute it within any Java-enabled web browser, or use the appletviewer provided in the JDK. Within the directory containing YourApplet, enter the command:
appletviewer YourApplet.html
If you are using a product other than JDK, follow its instructions to acheve the same result.
Example of Java Application
Try creating and running a Java application as described above using the following code.
public class MyFirstApp
{
public static void main(String[] args)
{
String[] casts =
{
"you will become rich" , "you will be healthy",
"you will get married" , "you will change jobs",
"you will get promoted" , "there will be an addition to your family",
"you will meet a tall dark handsome stranger"
};
System.out.println("Your forecast for today is that "
+ casts[(int)( casts.length*Math.random())%7]);
}
}
