Input streams kill my applet

This is probably a little thing that I have forgotten but I can't for the life of me get. Ok I should probably say I am running XP, with Java 1.5 and Firefox

I have an applet that works fine on Eclipse but when I try to run it by it self it seems to die at the point where I have my input streams.

Basicly I have included the test code that removes all the input processing and tree building. This test script below just opens a input stream, creates a tree with two nodes and displays the tree. If you remove any the 3 TRYS it will not work.

I have tried various methods to read in the URL I have but it seems to die every time. I have included the code below. If anyone has any suggestions I would greatly appreciate it.

Thanks

Luis

import java.applet.AppletContext;

import java.awt.Color;

import java.awt.Font;

import java.io.DataInputStream;

import java.io.InputStream;

import java.net.*;

import java.io.*;

import javax.swing.JApplet;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JTree;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreePath;

import java.net.URLConnection;

publicclass TestAppletextends JApplet{

// Variables used by the applet

protected JTree jt;

protected URL url;

publicvoid init(){

setBackground(Color.lightGray);

setFont(new Font("Verdana", Font.BOLD, 12));

try{

url =new URL("http://www.yahoo.com");

//IF I REMOVE ANY OF THE FOLLOWING COMMENTS IT DOES NOT WORK

//TRY 1. IF REMOVED IT WILL NOT WORK

/*URL yahoo = new URL("http://www.yahoo.com/");

BufferedReader in = new BufferedReader(yahoo.openStream());

String inputLine;

while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);

in.close();

*/

//TRY 2. IF REMOVED IT WILL NOT WORK

/*URLConnection connection = url.openConnection();

InputStream in = connection.getInputStream();

DataInputStream stream = new DataInputStream(in);

*/

//TRY 3. IF REMOVED IT WILL NOT WORK

//BufferedReader stream = new BufferedReader(new InputStreamReader(url.openStream()));

DefaultMutableTreeNode rootNode =new DefaultMutableTreeNode(url);

rootNode.add(new DefaultMutableTreeNode("Child 1"));

JTree jt =new JTree(rootNode);

// Tree Setup

jt.setExpandsSelectedPaths(true);

jt.setVisible(true);

jt.putClientProperty("JTree.lineStyle","Angled");

getContentPane().removeAll();

getContentPane().add(jt);

//stream.close();

//iStream.close();

}catch (Exception e){

e.printStackTrace();

}

}

publicvoid start(){

}

}// end TreeApplet Class

[4866 byte] By [lbarberia] at [2007-10-3 5:22:05]
# 1

> I have an applet that works fine on Eclipse

Eclipse runs the applet with appletviewer and an allpermission policy.

Since an applet cannot connect to a server other than the server where it

came from you probably get a securityException or AccessControlExceprion.

And because your code is in init it looks like your applet hangs (applet not inited).

Try to put your URL code in start or in a seporate thread. Try to sign.set policy for your applet

so it is allowed to connect to hosts other than the one it came from.

Signing applets:

http://forum.java.sun.com/thread.jsp?forum=63&thread=524815

second post and reply 18 for the java class file using doprivileged

Still problems?

A Full trace might help us out:

http://forum.java.sun.com/thread.jspa?threadID=656028

harmmeijera at 2007-7-14 23:29:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

You are right on the money about the applet viewer working and the security errors I am getting. Look below this is from the console.

java.security.AccessControlException: access denied (java.net.SocketPermission www.reservhotel.com:80 connect,resolve)

at java.security.AccessControlContext.checkPermission(Unknown Source)

at java.security.AccessController.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkConnect(Unknown Source)

at sun.net.www.http.HttpClient.openServer(Unknown Source)

at sun.net.www.http.HttpClient.<init>(Unknown Source)

at sun.net.www.http.HttpClient.New(Unknown Source)

at sun.net.www.http.HttpClient.New(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

at RHMenu.start(RHMenu.java:80)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

I understand that the applet cannot connect to a server other than the one it is connected to but shouldn't I be able to connect to "www.yahoo.com"? I tried putting the URL and open stream in the start thread but it does not seem to work. When I run it in a web browser, the Java Sun comes up and it says it is inited and started but nothing comes up. Any ideas?

Thanks,

Luis

lbarberia at 2007-7-14 23:29:06 > top of Java-index,Desktop,Core GUI APIs...