Problem embedding an applet in an HTML page

I created a simple JApplet GUI using NetBeans that I want to read from a text file. Here is the simplified code I am using to debug:

private void okButtonActionPerformed(java.awt.event.ActionEvent evt){

String input = field1.getText();

label1.setText(input);

try {

BufferedReader in = new BufferedReader(new FileReader("C:\\Temp\\file.txt"));

String str;

while ((str = in.readLine()) != null) {

label2.setText(str);

}

in.close();

} catch (IOException e) {

label2.setText("Error");

}

}

When I run it in NetBeans, it works perfectly, but when I use the HTML file that is automatically created by NetBeans, the following happens:

-The applet loads fine and looks to be functioning correctly.

-Anything that I put before the "try" will run correctly.

-Once it hits the "try", it does nothing - it does not even execute the exception code. It is like everything from the "try" on is not even there.

-I get no errors.

To be clear, when I run the applet in NetBeans, it works correctly - it can read from the file and set display fields. But when I run the applet in the HTML file that was created by NetBeans, it will not execute anything after the "try", but does not generate any errors.

I have also tried creating an HTML file from scratch and embedding the applet using the APPLET tag, and I get the same result.

[1449 byte] By [mogulskier360a] at [2007-11-27 7:27:51]
# 1

Applets are not allowed to read files from the local drive; I'm guessing netbeans is running it such that the "sandbox" that an applet runs in is not applied, but under normal execution an applet is severely limited in what it can do to the local system.

Just think about it, if an applet can work with files on the local harddisk, you can go to any website with an applet and have it erase all files on your harddrive... I certainly would not want that.

Probably the applet is throwing a security exception, not an IOException. Try changing to code to

} catch (Exception e) {

label2.setText(e.getMessage());

}

and see what happens. Also look into "signing" the applet, that will give an applet more rights.

gimbal2a at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 2
> Also look into "signing" the applet, that will give an applet more rights.Or you may find it more straightforward to change your applet into a Java application, where the security issues do not exist. Trying to deal with those issues is going to distract you from learning
DrClapa at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 3

> I created a simple JApplet GUI using NetBeans that I

> want to read from a text file. Here is the simplified

> code I am using to debug:

>

> private void

> okButtonActionPerformed(java.awt.event.ActionEvent

> evt){

> String input = field1.getText();

> label1.setText(input);

>

> try {

> BufferedReader in = new

> BufferedReader(new

> FileReader("C:\\Temp\\file.txt"));

> String str;

> while ((str = in.readLine()) != null) {

>label2.setText(str);

> in.close();

> tch (IOException e) {

> label2.setText("Error");

>

> , it works perfectly, but when I use the HTML file

> that is automatically created by NetBeans, the

> following happens:

>

> -The applet loads fine and looks to be functioning

> correctly.

> -Anything that I put before the "try" will run

> correctly.

> -Once it hits the "try", it does nothing - it does

> not even execute the exception code. It is like

> everything from the "try" on is not even there.

> -I get no errors.

>

> To be clear, when I run the applet in NetBeans, it

> works correctly - it can read from the file and set

> display fields. But when I run the applet in the HTML

> file that was created by NetBeans, it will not

> execute anything after the "try", but does not

> generate any errors.

>

> I have also tried creating an HTML file from scratch

> and embedding the applet using the APPLET tag, and I

> get the same result.

Applets are not allowed to access sensitive client system resources,

such as files. This is because of security restrictions. Otherwise,

while visiting an hostile site, users can unknowingly download an

applet that erases their hard-disk. Please visit this for more information:

http://java.sun.com/sfaq/.

Your dangereous line (FILE ACCESS!) is

BufferedReader in = new BufferedReader(new FileReader("C:\\Temp\\file.txt"));

It does not throw an IOException but an uncaught SecurityException.

Use the Java console of your Java plug-in to see it.

Netbeans allows you to develop applets, but it does not implement the

security 'sand-box' as browsers do.

Check also the Signed Applets forum.

My link above is quite old stuff. Search around for

"applet security" for more updated info.

Message was edited by:

baftos

baftosa at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks for your help, that clears a lot of things up.

My end goal is to be able to write a program that accesses a locally-stored database. I have used programs before that were embedded in web pages, and I liked how I could run them from a computer with nothing on it just by running a browser which will prompt the user to install Java.

Is there a way for me to accomplish this functionality while being able to access a local database?

mogulskier360a at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 5

Are you considering using an applet just because it makes it easier for you to manage the installation of your application? Does your application really need to be downloaded from the web every time it's used? Does running it from the web add any value compared to just installing it on the client's system?

DrClapa at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 6

I want to have the application stored on an intranet so it can be accessed by any computer on the network. I liked that, with an applet, a new client with nothing on it could just open the HTML file, which would prompt the user to install Java, and it could run the program with no setup. I have used programs that are set up like this before, and I would like to be able to do it with this application.

mogulskier360a at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 7

> I want to have the application stored on an intranet

> so it can be accessed by any computer on the network.

OK, there are two ways to give your applet privileges:

signing it or playing with the security policy of each of your

clients. The security policy is probably easier and quite

feasable on an intranet. You can find lots of good advice

in the Signed Applets forum from people who know the

subject better than me.

baftosa at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 8
> My end goal is to be able to write a program that accesses a locally-stored database.So there will be separate databases on each client machine?!
Hippolytea at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 9
No, each instance of the application would access the same database on a networked server.
mogulskier360a at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...
# 10
I found a way to do it here: http://osdir.com/ml/java.hsqldb.user/2003-01/msg00016.htmlUsing this, I am able to allow permissions for a specific applet, or allow all applets to access a specific file and set read and write access to that file.Thanks for all your help.
mogulskier360a at 2007-7-12 19:08:06 > top of Java-index,Java Essentials,New To Java...