dirku
asked on
Running Swing applets in IE or Navigator
I have a problem running HTML pages containing the APPLET-tag which causes to run an applet using Swing.
My program works well in the AppletViewer but ti does not in Internet Explorer(4.0) nor in Netscape Navigator(4.05).
How can I solve this problem?
In the AUTOEXEC.BAT I have set the following environment variables:
SET JAVA_HOME=C:\JAVA\JDK1.1.6
SET SWING_HOME=%JAVA_HOME%\SWI NG-1.0.2
SET JDBCHOME=%JAVA_HOME%\LIB\c lasses.zip
SET CLASSPATH=.;%JAVA_HOME%\LI B\classes. zip;%SWING _HOME%\src .zip;%SWIN G_HOME%\sw ingall.jar
SET PATH=%PATH%;%JAVA_HOME%\BI N;C:\JAVA\ IDE\KAWA30
...but how can a user run my JApplet in a browser? Must he set some environment variables, too?
I think that a user just wants to start the browser and then wants the applets run! ->Without setting any viariables!
I even copied the swingall.jar file to the C:\Windows\java\classes directory and to the java\classes directory of the Communicator:
Communicator shows: "error:java.lang.NoSuchMet hodError: java.awt.Component: method enableEvents(J)V not found"
IE: NullPointerException
Thank you.
ulrich@tembit.com
My program works well in the AppletViewer but ti does not in Internet Explorer(4.0) nor in Netscape Navigator(4.05).
How can I solve this problem?
In the AUTOEXEC.BAT I have set the following environment variables:
SET JAVA_HOME=C:\JAVA\JDK1.1.6
SET SWING_HOME=%JAVA_HOME%\SWI
SET JDBCHOME=%JAVA_HOME%\LIB\c
SET CLASSPATH=.;%JAVA_HOME%\LI
SET PATH=%PATH%;%JAVA_HOME%\BI
...but how can a user run my JApplet in a browser? Must he set some environment variables, too?
I think that a user just wants to start the browser and then wants the applets run! ->Without setting any viariables!
I even copied the swingall.jar file to the C:\Windows\java\classes directory and to the java\classes directory of the Communicator:
Communicator shows: "error:java.lang.NoSuchMet
IE: NullPointerException
Thank you.
ulrich@tembit.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Running a simple test program (you can see the text below) it results in an AppletSecurityException when running Windos95. Running the program under Windows NT there are no problems.
Why? What can I do to make it run on both OS?
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class Test extends JApplet implements ActionListener
{
private JButton start = new JButton("Start");
private JComboBox symbols = new JComboBox();
public void init()
{
getContentPane().setLayout (new BorderLayout());
getContentPane().add(Borde rLayout.CE NTER, symbols);
getContentPane().add(Borde rLayout.SO UTH, start);
start.addActionListener(th is);
}
public void start()
{
setVisible(true);
}
public void destroy()
{
}
public void loadSymbols()
{
URL url = null;
try
{
url = new URL(getCodeBase(), "tcx_data/day/ABB.day");
System.out.println(url);
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
while ( (line = br.readLine()) != null)
{
System.out.println(line);
symbols.addItem(line);
}// end while
}
catch(MalformedURLExceptio n MURLe)
{
System.err.println("URL nicht korrekt!");
System.err.println(MURLe);
}
catch(IOException IOe)
{
System.err.println("Konnte Datei nicht lesen!");
System.err.println(IOe);
}
}//end loadSymbols
public void actionPerformed(ActionEven t Ae)
{
System.out.println(Ae.toSt ring());
if( Ae.getSource().equals(star t) )
{
System.out.println("Start gedrückt...");
loadSymbols();
}
}
}//end class Test
Why? What can I do to make it run on both OS?
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class Test extends JApplet implements ActionListener
{
private JButton start = new JButton("Start");
private JComboBox symbols = new JComboBox();
public void init()
{
getContentPane().setLayout
getContentPane().add(Borde
getContentPane().add(Borde
start.addActionListener(th
}
public void start()
{
setVisible(true);
}
public void destroy()
{
}
public void loadSymbols()
{
URL url = null;
try
{
url = new URL(getCodeBase(), "tcx_data/day/ABB.day");
System.out.println(url);
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
while ( (line = br.readLine()) != null)
{
System.out.println(line);
symbols.addItem(line);
}// end while
}
catch(MalformedURLExceptio
{
System.err.println("URL nicht korrekt!");
System.err.println(MURLe);
}
catch(IOException IOe)
{
System.err.println("Konnte
System.err.println(IOe);
}
}//end loadSymbols
public void actionPerformed(ActionEven
{
System.out.println(Ae.toSt
if( Ae.getSource().equals(star
{
System.out.println("Start gedrückt...");
loadSymbols();
}
}
}//end class Test
ASKER
This happens when using Netscape Navigator 4.05, using MS IE 4.0 the program runs although there are numerous exceptions appearing in the Java console.
How can I make my programs run with both Netscape Navigator/Communicator and MS Internet Explorer considering Windows NT AND Windows 95 (of course, all other platforms, too)?
How can I make my programs run with both Netscape Navigator/Communicator and MS Internet Explorer considering Windows NT AND Windows 95 (of course, all other platforms, too)?
The applet looks reasonable. Could you turn the debugging on in the Java console and send me the output which gets produced when you run the applet. Make sure you clear browser's cache before running the applet and turn the debug mode on on the console.
ASKER
Hi, tought you have given it up to teach me :-))
Now I have installed the Personal Web Server and the test program runs well with it. However, I want to know what was wrong. I think if all the files are on my PC the program should have run, too, shouldn't it?
After starting the Navigator the program with the source above was the only I have run:
Netscape Communications Corporation -- Java 1.1.5
Type '?' for options.
Symantec Java! ByteCode Compiler Version 210.065
Copyright (C) 1996-97 Symantec Corporation
# Applet debug level set to 9
# initApplet: contextID=2 appletID=13737648 parentContext=12310688 frameContext=12310688
# New applet context: file:///C|/Java/IDE/Kawa30 /Projects/ Test/Defau lt.html, contextID=2
# Initializing applet: Test.class, appletID=13737648, contextID=2
# docURL=file:///C|/Java/IDE /Kawa30/Pr ojects/Tes t/Default. html reloadClasses=false
# total applets=1
# New applet: 13737648 at file:/C|/Java/IDE/Kawa30/P rojects/Te st/ width=300 height=300 hspace=0 vspace=0 align=baseline codebase=file:///C|/Java/I DE/Kawa30/ Projects/T est/ code=Test.class
# startApplet: contextID=2 appletID=13737648 newFrameMWContext=12310688
# Starting applet: Test, appletID=13737648, contextID=2
# new class loader netscape.applet.AppletClas sLoader@80 0e4c6
# context = netscape.applet.MozillaApp letContext @800c659
# codebase = file:/C|/Java/IDE/Kawa30/P rojects/Te st/
Swing: checked access to system event queue.
# Applet Test loaded
# Applet Test initialized
# Applet Test running
java.awt.event.ActionEvent [ACTION_PE RFORMED,cm d=Start] on com.sun.java.swing.JButton [,0,275,30 0x25,layou t=com.sun. java.swing .OverlayLa yout]
Start gedrückt...
file:/C|/Java/IDE/Kawa30/P rojects/Te st/tcx_dat a/day/ABB. day
netscape.security.AppletSe curityExce ption: security.class from local disk trying to access url: file:/C|/Java/IDE/Kawa30/P rojects/Te st/tcx_dat a/day/ABB. day
at java.lang.Throwable.<init> (Compiled Code)
at java.lang.Exception.<init> (Compiled Code)
at java.lang.RuntimeException .<init>(Co mpiled Code)
at java.lang.SecurityExceptio n.<init>(C ompiled Code)
at netscape.security.AppletSe curityExce ption.<ini t>(Compile d Code)
at netscape.security.AppletSe curityExce ption.<ini t>(Compile d Code)
at netscape.security.AppletSe curity.che ckURLConne ct(Compile d Code)
at java.lang.SecurityManager. checkURLCo nnect(Comp iled Code)
at netscape.net.URLConnection .connect(C ompiled Code)
at netscape.net.URLConnection .getInputS tream(Comp iled Code)
at java.net.URL.openStream(Co mpiled Code)
at Test.loadSymbols(Compiled Code)
at Test.actionPerformed(Compi led Code)
at com.sun.java.swing.Abstrac tButton.fi reActionPe rformed(Co mpiled Code)
at com.sun.java.swing.Abstrac tButton$Fo rwardActio nEvents.ac tionPerfor med(Compil ed Code)
at com.sun.java.swing.Default ButtonMode l.fireActi onPerforme d(Compiled Code)
at com.sun.java.swing.Default ButtonMode l.setPress ed(Compile d Code)
at com.sun.java.swing.plaf.ba sic.BasicB uttonListe ner.mouseR eleased(Co mpiled Code)
at java.awt.Component.process MouseEvent (Compiled Code)
at java.awt.Component.process Event(Comp iled Code)
at java.awt.Container.process Event(Comp iled Code)
at java.awt.Component.dispatc hEventImpl (Compiled Code)
at java.awt.Container.dispatc hEventImpl (Compiled Code)
at java.awt.Component.dispatc hEvent(Com piled Code)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Compil ed Code)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Compile d Code)
at java.awt.LightweightDispat cher.dispa tchEvent(C ompiled Code)
at java.awt.Container.dispatc hEventImpl (Compiled Code)
* at java.awt.Component.dispatc hEvent(Com piled Code)
at java.awt.EventDispatchThre ad$EventPu mp.dispatc hEvents(Co mpiled Code)
at java.awt.EventDispatchThre ad.run(Com piled Code)
at netscape.applet.DerivedApp letFrame$A ppletEvent DispatchTh read.run(C ompiled Code)
Besides: What's the exactly distinction between getDocumentBase() and getCodeBase()???
Now I have installed the Personal Web Server and the test program runs well with it. However, I want to know what was wrong. I think if all the files are on my PC the program should have run, too, shouldn't it?
After starting the Navigator the program with the source above was the only I have run:
Netscape Communications Corporation -- Java 1.1.5
Type '?' for options.
Symantec Java! ByteCode Compiler Version 210.065
Copyright (C) 1996-97 Symantec Corporation
# Applet debug level set to 9
# initApplet: contextID=2 appletID=13737648 parentContext=12310688 frameContext=12310688
# New applet context: file:///C|/Java/IDE/Kawa30
# Initializing applet: Test.class, appletID=13737648, contextID=2
# docURL=file:///C|/Java/IDE
# total applets=1
# New applet: 13737648 at file:/C|/Java/IDE/Kawa30/P
# startApplet: contextID=2 appletID=13737648 newFrameMWContext=12310688
# Starting applet: Test, appletID=13737648, contextID=2
# new class loader netscape.applet.AppletClas
# context = netscape.applet.MozillaApp
# codebase = file:/C|/Java/IDE/Kawa30/P
Swing: checked access to system event queue.
# Applet Test loaded
# Applet Test initialized
# Applet Test running
java.awt.event.ActionEvent
Start gedrückt...
file:/C|/Java/IDE/Kawa30/P
netscape.security.AppletSe
at java.lang.Throwable.<init>
at java.lang.Exception.<init>
at java.lang.RuntimeException
at java.lang.SecurityExceptio
at netscape.security.AppletSe
at netscape.security.AppletSe
at netscape.security.AppletSe
at java.lang.SecurityManager.
at netscape.net.URLConnection
at netscape.net.URLConnection
at java.net.URL.openStream(Co
at Test.loadSymbols(Compiled Code)
at Test.actionPerformed(Compi
at com.sun.java.swing.Abstrac
at com.sun.java.swing.Abstrac
at com.sun.java.swing.Default
at com.sun.java.swing.Default
at com.sun.java.swing.plaf.ba
at java.awt.Component.process
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
* at java.awt.Component.dispatc
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at netscape.applet.DerivedApp
Besides: What's the exactly distinction between getDocumentBase() and getCodeBase()???
The message:
# New applet context: file:///C|/Java/IDE/Kawa30 /Projects/ Test/Defau lt.html
Means that you are using file: protocol to load the applet. In this case the browser will not allow you to read files. You have to use http: protocol to load your applet (from your web server). Then the codebase will also have http: as its protocol an you should be able to read the file.
# New applet context: file:///C|/Java/IDE/Kawa30
Means that you are using file: protocol to load the applet. In this case the browser will not allow you to read files. You have to use http: protocol to load your applet (from your web server). Then the codebase will also have http: as its protocol an you should be able to read the file.
ASKER
In Netscape Navigaotr I've got it run but now I have a further question. I want to do the following:
I load some data from a database into the JApplet. This works well. Then I want to update the entries of the JTable used within this JApplet. Therefore I want to read out a file from another host. I create a URL using: URL url = new URL(getDocumentBase(), "data/myFile.txt");
BufferedReader br = new BufferedReader(new FileReader(url));
try
{...}
Now I have an AppletSecurityException which says that reading of this file is not permitted.
How can I get permission to read out this file?