Applications and Applets
Up and Running
The code presented here isn't meant to help you understand or learn Java. It's just to ensure that you hava Java set up properly on your computer. If this code compiles and runs, you're sorted.
Application
To get an application running, here's what you need to do. Open up notepad, or any text editor and copy and paste the following code:
//A simple test application
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestApp extends JFrame {
public TestApp ( ) {
super ( "Sorted ");
Container c = getContentPane( );
JLabel text = new JLabel ( "Looking good", SwingConstants.CENTER );
c.add ( text );
setSize ( 300, 200 );
show ( );
}
public static void main ( String args [ ] ) {
TestApp testApp = new TestApp ( );
testApp.addWindowListener (
new WindowAdapter ( ) {
public void windowClosing ( WindowEvent e )
{ System.exit ( 0 ); }
}
);
}
} // end of class TestApp
Now save the file as
"TestApp.java"
making sure you include the quotation marks and get the capitalisation right. ( When you save a Java file, the file
must have exactly the same name as the class name ). Open up a DOS window or from the command line, type:
javac c:\code\TestApp.java
where c:\code is the location of the file you've just saved. Everything should work fine and TestApp.class should be created, although you won't be told about this. If javac exits without any errors, check the directory where TestApp is located and you'll find that a TestApp.class now exists too. TestApp.class contains the bytecode that the JVM (Java Virtual Machine) executes.
To run this application all you have to is go back to your command line or DOS prompt and type:
java -classpath c:\code TestApp
If all goes well a small window will open that says "Looking good" in the middle. You can close the window by clicking the X in the top-right corner. If you decide to include TestApp in a package then the fully qualified name up to the highest package directory must be specified using "." instead of slashes. So for class 'TestApp' in package 'myPackage' in directory 'code' you'd type:
java -classpath c:\code myPackage.TestApp
Applets
To get an applet running we follow the same steps as last time - open up notepad or any text editor and copy and paste the following code:
import java.awt.*;
import javax.swing.*;
public class TestApplet extends JApplet {
public void paint ( Graphics g ) {
g.drawString ( "Applets work too!", 50, 50);
}
} // end of class TestApplet
Now save the file as "TestApplet.java" again making sure you include the quotation marks and get the capitalisation right. Open up a DOS window or from the command line, type:
javac c:\code\TestApp.java
where c:\code is the location of the file you've just saved. Same as last time TestApplet.class should be created, but because this file is an applet we use appletviewer to run it. Appletviewer runs on HTML files so we need to create one of those too.
Again, open up notepad or any text editor and copy and paste the following code:
<HTML>
<APPLET code="TestApplet.class" width = 300 height = 150>
</APPLET>
</HTML>
You can use any name you want for the HTML file, let's just save it as "web.html" . The reason you use the quotation marks is so that your text editor doesn't save it as web.html.txt or some such bullshit. Make sure that web.html and TestApp.java are saved in the same folder. To get the applet running here's what you type:
appletviewer file:\\\c:\code\web.html
As appletviewer really takes an URL as argument, you'll have to specify the full URL if the HTML file is on another drive or you just want to specify the drive.
Note: The three slashes after file: are essential.
More simply, if using a DOS prompt you can cd to the directory where web.html is stored and just type:
appletviewer web.html
A small window should open up informing you that applets work too.
If you've succeeded in getting both the application and applet to run you can be happy in the knowledge that Java is happily installed on your system. There's one last thing to check though. Try opening your web.html in your browser. If your browser is old it mighn't work properly. That's nothing to worry about, take a look here to get that sorted out.
If the code doesn't work, maybe you get errors like java.lang.NoClassDefError or something like that, there's a few things you can check. Take a look here.
Swinging Browser
Menu
Last updated August 15th 2000 |