Converting an application to an Applet

Hi,
   just wondering if anyone could help me with this one.  I've just completed an application in Java.  The initial screen of the application is a Logon screen and it extends JFrame.  I've been asked to now convert the app. to a web based app.  I wrote an initial java applet file like the following:

import javax.swing.JApplet;

public class AppletLogon extends JApplet  {

      public void init() {
            
            Logon appBegin = new Logon();
            
      }
}


And then wrote in the html file:
<html>
<head>
<TITLE>Catalogue System</TITLE>
</head>

<body>

<APPLET CODE="AppletLogon.class" WIDTH=1000 HEIGHT=500> </APPLET>

<body>

<html>

My application as it stands when passed the Logon Frame displays a JFrame with a menu that can display
JInternalFrames depending on what is selected from the menu.

When I run the the Applet it just comes up all blank with:
Applet AppletLogon notinited in the status bar at the bottom of the web page

Anyone got any ideas?
Any help would be grateful

V
BourkeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nesnemisCommented:
Hi Bourke,

change your jframe into jpanel and just add the panel

nesnemis
0
girionisCommented:
What does the applet console say?
0
BourkeAuthor Commented:
I dont know what you mean by applet console?

Just at the end of the web page on the status bar it says "Applet AppletLogon notinited"
There is just a large grey box where the applet should display.

If I change the JFrame in the Logon.java to a panel, is it ok to leave the JFrame in the
java file that has the menu and other files that are JInternalFrame's ?????
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

girionisCommented:
There should bne a console that displays the error message. What happens if you try to run it with the appletviewer?
0
BourkeAuthor Commented:
How do you run appletviewer? sorry about this but I dont know much about applets.

I also went and created a new Java file that includes a JPanel and did the following:

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;
import javax.swing.*;

public class AptLogon2 extends JApplet  {

      Logon appBegin;
      JPanel p = new JPanel();
      
      public void init() {
            
            Logon appBegin = new Logon();
            p.add(appBegin);
            getContentPane().add(p, BorderLayout.CENTER);
      }
}
but this still didnt work, the file Logon.java extends JFrame
0
girionisCommented:
>  How do you run appletviewer?

Simply do:

appletviewer <your html file that has the applet>
0
BourkeAuthor Commented:
Hi just did that there and I got the following error in the console:

java.security.AccessControlException: access denied (java.lang.RuntimePermission
 accessClassInPackage.sun.jdbc.odbc)
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
        at java.security.AccessController.checkPermission(AccessController.java:401)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
        at java.lang.SecurityManager.checkPackageAccess(SecurityManager.java:1491)
        at sun.applet.AppletSecurity.checkPackageAccess(AppletSecurity.java:190)

        at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:141)
        at Database.DBConnection.Connect(DBConnection.java:24)
        at Logon.<init>(Logon.java:59)
        at AptLogon2.init(AptLogon2.java:15)
        at sun.applet.AppletPanel.run(AppletPanel.java:353)
        at java.lang.Thread.run(Thread.java:534)

In the Logon.java I'm connecting to a database, would that cause a problem?

V
0
girionisCommented:
Where is the databases running? Is it in the same server wheer your applet is also running? If not you will either need to sign the applet or modify the java.policy file. For more info on both topics have a look here:

http://forum.java.sun.com/thread.jsp?forum=63&thread=174214&message=548253
http://java.sun.com/security/signExample12/
http://java.sun.com/j2se/1.3/docs/guide/security/PolicyFiles.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BourkeAuthor Commented:
The database is on the same machine my applet is running.  In the long term I dont think this
will be the case.  I assume the database in the end will be on a server and the applet might be on
another, I will have a DSN set up on the machine that has the applet so that it can connect to the
database.

Can this be done without me making much changes to my code at the moment?
0
girionisCommented:
You have to change the java.policy file then and give thsi permission: java.lang.RuntimePermission
 accessClassInPackage.sun.jdbc.odbc
0
girionisCommented:
... or sign the applet as I said in my previous psot.
0
BourkeAuthor Commented:
In order to sign the applet I need the applet to be stored in a jar file? if I create the .jar file it doesn't seem
to function properly.
How do I change the java.policy file?

Thanks for you help
V
0
girionisCommented:
> In order to sign the applet I need the applet to be stored in a jar file?

Not necessarily. You will just need to import a certificate (it can be a test, it does not matter) regrdless if the applet is in a jar file or not.

> In order to sign the applet I need the applet to be stored in a jar file?

Not necessarily. You will just need to import a certificate (it can be a test, it does not matter) regrdless if the applet is in a jar file or not.

> How do I change the java.policy file?

Edit the java.policy file and inside the grant {} block of code add this line:

java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";
0
BourkeAuthor Commented:
Hi,
  I did a search for java.policy file I got the following returned:

          C:\Program Files\ecc\console\jre\latest\lib\security
          C:\Program Files\j2sdk_nb\j2sdk1.4.2\jre\lib\security
          C:\Program Files\j2sdk_nb\_jvm\lib\security
          C:\Program Files\Java\j2re1.4.1_01\lib\security
          C:\Program Files\Java\j2re1.4.1_03\lib\security

any idea which one should be changed? I changed the second and last one but it made no difference

how do you import a certificate?

V
0
girionisCommented:
> C:\Program Files\Java\j2re1.4.1_01\lib\security
>           C:\Program Files\Java\j2re1.4.1_03\lib\security

It' either one of these two. Depends on the java version you are using. Do a: java -version and see what it says. Then change the appropriate policy file.
0
girionisCommented:
> how do you import a certificate?

Have a look at the links I posted about signing the applet. It explains how you can do it.
0
BourkeAuthor Commented:
Sorry being silly now, the policy file to change was in the applet viewer console.  I changed that one
anyway and didnt seem to work.  Do I have to do anything else other than just change the policy file?

V
0
BourkeAuthor Commented:
I edited the right policy file and now I'm getting:

java.security.AccessControlException: access denied (java.util.PropertyPermissn file.encoding read)
        at java.security.AccessControlContext.checkPermission(AccessControlConxt.java:269)
        at java.security.AccessController.checkPermission(AccessController.jav401)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
        at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:76)
        at java.lang.System.getProperty(System.java:573)
        at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:42)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
        at java.sql.DriverManager.getConnection(DriverManager.java:512)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at Database.DBConnection.Connect(DBConnection.java:38)
        at Logon.<init>(Logon.java:59)
        at AptLogon2.init(AptLogon2.java:15)
        at sun.applet.AppletPanel.run(AppletPanel.java:353)
        at java.lang.Thread.run(Thread.java:534)
0
BourkeAuthor Commented:
I found this alreayd in the policy file:
              permission java.lang.RuntimePermission "stopThread"
So was it ok to take this out?
0
girionisCommented:
You have to give all permissions necessary. Do the same as you did with the database permissions.
0
objectsCommented:
Use a type 3/4 jdbc driver.
0
BourkeAuthor Commented:
Hi,
   I didnt do anything with the database permissions?

I'm using
Driver Name: JDBC-ODBC Bridge (SQLSRV32.DLL)
Driver Version: 2.0001 (03.80.0194)

How would driver 3/4 make a diff?
0
BourkeAuthor Commented:
would the driver be installed on my local machine? or on
the server where the database is?

The above in I posted is where the database will be.
But at the moment I am running the app. and the database in the same machine.

Thanks
V
0
girionisCommented:
Type 3/4 drivers do not need database components to be installed on the client computer and therefore the applet would not have to have permissions to access client-side objects.
0
girionisCommented:
>  would the driver be installed on my local machine? or on
> the server where the database is?

They will be on the server.
0
BourkeAuthor Commented:
Found out what I am using!

Driver Name: JDBC-ODBC Bridge (odbcjt32.dll)
Driver Version: 2.0001 (04.00.6200)
0
girionisCommented:
It is not a database issue any more. It is a PropertyPermission issue
0
BourkeAuthor Commented:
I had a look on microsoft to try find 3/4 drivers dont seem to be able to locate them.  Any ideas?
0
BourkeAuthor Commented:
I'm using the the standard edition of java. Is that still ok
to use 3/4 drivers?  Have they to be purchased?

If they have I'll have to find another way around the problem?
0
girionisCommented:
>  I'm using the the standard edition of java. Is that still ok
> to use 3/4 drivers?

Yes.

> Have they to be purchased?

Depends. Some of them are free some of them require a license.
0
BourkeAuthor Commented:
Hi,
   dont think this route is an option as it brought up a list of five vendors and they only have trials for 30 days  or
you have to buy.

What is my best option from here do you think, excuse all the questions.

V
0
girionisCommented:
> What is my best option from here do you think, excuse all the questions.

I guess you are using MSAccess database? My suggestion is to move on to a serious and compeltely free database like MySQL (http://www.mysql.org) which is far superior to access and the database drivers are free. Otherwise you can go on with MSAccess, type 1 drivers and sign the applet (this will also require an annual fee).
0
BourkeAuthor Commented:
So changing the policy.java file definately wont work do you think?
0
BourkeAuthor Commented:
Am still getting this:

java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
        at java.security.AccessController.checkPermission(AccessController.java:401)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
        at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1276)
        at java.lang.System.getProperty(System.java:573)
        at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:142)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
        at java.sql.DriverManager.getConnection(DriverManager.java:512)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at Database.DBConnection.Connect(DBConnection.java:38)
        at Logon.<init>(Logon.java:59)
        at AptLogon2.init(AptLogon2.java:15)
        at sun.applet.AppletPanel.run(AppletPanel.java:353)
        at java.lang.Thread.run(Thread.java:534)
0
girionisCommented:
It will work but you will have to do for every computer that your applet will run onto. Changing the java.policy file is an acceptable solution if you intent to run your applet on your local computer or on the computers in your copmpany's intranet. But for usage over the internet you cannot visit every client and change their java.policy file so a certificate should be issued (=you will sign the applet).
0
girionisCommented:
> Am still getting this:
>
> java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)

As I said, you still need to change your java.policy file for *every* permission error you are getting, or sign the applet.
0
BourkeAuthor Commented:
The applet is intended to be used on the companies intranet.  But I cant go around every computer in the company either and change their policy file can I?
Or is it enough to just change the policy file on the computer where everything will sit, bar the database that will be on another server.

To be honest I dont undertand the errors that are being thrown up.  All the entries in the java.policy file
have "read" as the last, I dont know what to call it, I suppose parameter before the ; and after the ; is the next
permission.  Should I change the read to something else?
0
girionisCommented:
> Or is it enough to just change the policy file on the computer where everything will sit, bar the database
> that will be on another server.

Nope, you have to do it on every computer the applet will be loaded.

> To be honest I dont undertand the errors that are being thrown up.

The errors are thrown because applets run inside a sandbox and they are only allowed a limited access to client computers.
0
BourkeAuthor Commented:
Also on one part of the application I am sending an email and this uses
Runtime.exec to run a perl script on my computer to send the email.
I guess this will cause problems as well? Is there a way around that to?

Do I have this right, all the code is on one machine, the database is on another machine.
I place my html page somewhere on the company intranet(but all the code for my app here).  
If my applet is clicked on it runs on the client pc? so I thought accessing anything on the client pc would be a problem, but I'm not am I? I'm access the code on the application m/c and the database on the db server (which if I do what you suggested above should be ok) and the perl script is where all the other java classes are.
This is what will happen very soon but,  at the moment I have everything on my pc, so I dont understand that is not my pc the sandbox in this case?????
0
girionisCommented:
> I guess this will cause problems as well? Is there a way around that to?

Yes it will cause problems. Again you need to sing the applet/chaneg the policy file.

>Do I have this right, all the code is on one machine, the database is on another machine.
>I place my html page somewhere on the company intranet(but all the code for my app here).

If the db and the applet are in a different server you will need to also sign the applet even if you use type 4 database drivers.

> If my applet is clicked on it runs on the client pc?

Yes

> so I thought accessing anything on the client pc would be a problem

It is a problem, that's why you need to sing it.
0
BourkeAuthor Commented:
Also you mentioned above the signing the applet would incurr and annual fee.
I look at the link you gave me above and is this fee incurred when the is applet signed with a cert that has been verified by a CA source ?

0
BourkeAuthor Commented:
Ok so I could leave the database alone so with the drivers I currently have and just go with signing the applet?
0
girionisCommented:
> I look at the link you gave me above and is this fee incurred when the is applet signed with a cert that has been verified by a
> CA source ?

Yes.

>  Ok so I could leave the database alone so with the drivers I currently have and just go with signing the applet?

Yes
0
BourkeAuthor Commented:
I came across this on a web page:

If an applet resides on the client's local disk, and in a directory that is on the client's CLASSPATH, then it is loaded by the file system loader. The most important differences are

applets loaded via the file system are allowed to read and write files
applets loaded via the file system are allowed to load libraries on the client
applets loaded via the file system are allowed to exec processes
applets loaded via the file system are allowed to exit the virtual machine
applets loaded via the file system are not passed through the byte code verifier
Java-enabled browsers use the applet class loader to load applets specified with file: URLs. So, the restrictions and protections that accrue from the class loader and its associated security manager are now in effect for applets loaded via file: URLs.

Still cant grasp if everything is on my pc, according to the above it should work?
0
BourkeAuthor Commented:
Hi I put this line in the java.policy file:
               permission java.security.AllPermission;
Then I ran the applet viewer and it worked fine.  When I double clicked on the HTML file it didn't work
at all.  Would you have any idea?
Would I be better of using java web start? as opposed to what I am doing?
0
girionisCommented:
Normally the applet class is downloaded into the temp folder so it is unlikely that it will get loaded by the system classloader.

>Hi I put this line in the java.policy file:
>               permission java.security.AllPermission;

This gives all permissions to applet. Good for development but wouldn't trust it for production as it is a serious security threat.

> When I double clicked on the HTML file it didn't work at all.  Would you have any idea?

You need to tell me what the Java console says.

> Would I be better of using java web start? as opposed to what I am doing?

Same restrictions apply with web start as well.
0
BourkeAuthor Commented:
All the comes up on the web page is a large grey empty rectangle with a red X at the top left hand
corner.  I tried this out on my pc at home and I got something working, giving all permissions and
error messages did come up in the middle of the appet greyed out area.
Is there somthing I need to set in the browser settings to get the errors to show up?

thanks
0
girionisCommented:
It seems that you do not have the java plugin installed for your browser. I suggest you download and install it from http://java.sun.com
0
BourkeAuthor Commented:
Hi,
  I downloaded the newest version of JRE and installed it.  I then changed the policy file to give AllPermissions.
Now what happens is the web page is displayed and then my application logon screen is displayed not on the web page but on a seperate screen.  I logon on to my application and the main page with the menu options  replaces the Logon screen as a seperate screen as well.

Would you know how to get these onto the actual applet web page?  

My Logon page extends JFrame, my application main screen extends JFrame and any other Frames that
are menu options are JInternalFrames.

This is the contents of the class that is named in the html file:

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;
import javax.swing.*;

public class AptLogon2 extends JApplet  {
      
      Logon appBegin;
      
      public void init() {
            
            Logon appBegin = new Logon();
            
            getContentPane().add(appBegin, BorderLayout.CENTER);
            
      }
      
}

0
girionisCommented:
I am not sure I understand what exactly is going on. Do you laod the applet and all of a suddent a new windows appears? You might have target="_blank" somewhere in your code and you open a new window. Do you have your applet on a web server and a URL where I can see what exactly is going on?
0
BourkeAuthor Commented:
>>I am not sure I understand what exactly is going on. Do you laod the applet and all of a suddent a new >>windows appears?

Yes that is exactly what happens.

>>You might have target="_blank" somewhere

This is what the html file looks like:
<html>
<head>
<TITLE>Catalogue System</TITLE>
</head>

<body>

<APPLET code ="AptLogon2.class" WIDTH=1000 HEIGHT=500> </APPLET>

<body>

<html>

>>Do you have your applet on a web server and a URL where I can see what exactly is going on?

Unfortunately everything is just on my own PC.
0
girionisCommented:
I am not sure why it is doing it. Is the logon screen the first screen you should see when you load the applet?
0
BourkeAuthor Commented:
Yes, the Logon screen is the first screen I should see when I load the applet.
I dont know why it is opening another window, would it have to do with that
the Logon class extends JFrame?
0
BourkeAuthor Commented:
I found this on a web site:

Panels
Panels are containers that do not create separate windows of their own. A panel should be used as the main container for applets that display their GUIs in a browser page or applet viewer.

Frames
Frames are top-level application windows with a title and a border. A frame should be used as the main container for:

A standalone application
An applet whose GUI exists outside of the browser, not within the page

So basically what is happening is the last line above. So would it be correct to say that I would have to change my whole app as it extends from JFrame/JInternalFrame to now just have Panels?

Is there any other way to avoid doing this?
0
girionisCommented:
> So would it be correct to say that I would have to change my whole app as it extends from JFrame/JInternalFrame to now
> just have Panels?

No, you would be wrong. There is no need to change the JPanel to a JFrame. A standalone application is one that contains a public static void main method declaration. A standalone application does not need another component to run (a browser or a server) it runs on its own, it stands alone.

Your problem hasn't got to do with the JPanel (I would be *very* surprised if it did), there should be something else that is causing a new window to appear, maybe you open a new window in your code by using the appletContext.showDocument(). I cannot tell for certain if I do not see the behaviour happening.
0
BourkeAuthor Commented:
My Logon class extended JFrame and on top of that I put a JPanel with
all my other components on it.  I now extened my Logon class from JApplet so there was just left the JPanel and the other components to go on the panel.  This class will now display as an applet on the web page.
I enter in my username and password and it opens up another window, not a web page, with my main application page.

So as far as I can see this is what is happening:
"An applet whose GUI exists outside of the browser, not within the page"

From reading a few articles, JApplet hierarchy is:
java.lang.Object
  java.awt.Component
      java.awt.Container
          java.awt.Panel
              java.applet.Applet
                  javax.swing.JApplet

And JFrame hierarchy is:
java.lang.Object
  java.awt.Component
      java.awt.Container
          java.awt.Window
              java.awt.Frame
                  javax.swing.JFrame

So I cant really put a JFrame into a JApplet????
How do I put this, the common demoninator between the two is
java.awt.Container is there anyway I could use this?

No I dont use appletContext.showDocument at all.  All I have is JFrames/JInternalFrames.
Would you have a simple example of an applet that includes a class
that extends JFrame?

thanks
V

V
0
girionisCommented:
If you do not experience this problem when you run it from the appletviewerthen you shouldn't experience it when you run it from the web browser. Try a different browser and see what happens.
0
BourkeAuthor Commented:
Well the appletviewer does open up a seperate window as well.
0
girionisCommented:
Then this is normal behaviour. I was under the impression that the appletviewer didn't open up a new window. You will have to use the design of your applet then to not open up a new window.
0
BourkeAuthor Commented:
Would you have any idea how to do this? am I back to not using JFrames?
0
girionisCommented:
I would have to see the code in order to design it differently. Better post a new question for this.
0
BourkeAuthor Commented:
Ok then, well thank you for all you help, greatly appreciated.
V
0
girionisCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.