Debugging java applet in browser - works in Eclipse, but not in browser

I created an applet that opens JFileChooser to select a file by clicking JButton . It works great when I run it in Eclipse. When I embed it on an HTML page with an applet tag, nothing happens when I click a button.

Any suggestions as to why JFileChooser doesn't open in the browser would be appreciated , but my main question is: how do I debug this? I could not find anything on Google on how to add the Java console in Firefox 3.6 or Chrome. Is there a way to get some information on why JFileChooser is not opening?

Debugging replied in the comment below

So, the console says that there is an exception that excludes access, which, I assume, is due to the fact that I did not "sign" the applet. What should be the development process before signing the applet? Do I have to sign it with a certificate issued by a valid CA before I can verify it in the browser, or is there some simple thing that you can do by simply checking?

Here is my code:

 package com.putapplet; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.net.URL; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.SwingUtilities; @SuppressWarnings("serial") public class PutToS3Applet extends JApplet { private static long maxFileSizeInBytes = 1048576; private static String listenerUrl = "xxx"; private String listenerQueryString; private String signedPutUrl; private JProgressBar progressBar; private JButton button; private JLabel messageField; public void init() { setMaxFilesize(1); try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't complete successfully"); } } private void createGUI() { button = new JButton("Choose File..."); button.addActionListener(new ButtonListener()); progressBar = new JProgressBar(0, 100); progressBar.setStringPainted(true); messageField = new JLabel(); //messageField.setPreferredSize(new Dimension(300, 20)); FlowLayout layout = new FlowLayout(); layout.setAlignment(FlowLayout.LEFT); JPanel topPanel = new JPanel(); topPanel.setLayout(layout); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(layout); topPanel.add(button); topPanel.add(progressBar); bottomPanel.add(messageField); setLayout(new GridLayout(2,1)); add(topPanel); add(bottomPanel); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent ae) { JFileChooser fileChooser = new JFileChooser(); int showOpenDialog = fileChooser.showDialog(null, "Upload File"); if (showOpenDialog != JFileChooser.APPROVE_OPTION) return; final File fileToUpload = fileChooser.getSelectedFile(); if (fileToUpload.length() > PutToS3Applet.maxFileSizeInBytes) { messageField.setText("Your file must be smaller than " + getHumanReadableMaxFilesize()); return; } listenerQueryString = "query[filename]=" + fileToUpload.getName(); //get signed PUT url for s3 try { URL listener = new URL(listenerUrl + listenerQueryString); BufferedReader in = new BufferedReader(new InputStreamReader(listener.openStream())); signedPutUrl = in.readLine().replace("http://", "https://"); messageField.setText(signedPutUrl); } catch (Exception e) { messageField.setText("Oops, there was a problem connecting to the server. Please try again."); e.printStackTrace(); } } } private String getHumanReadableMaxFilesize() { return Long.toString((PutToS3Applet.maxFileSizeInBytes / 1024 / 1024)) + "MB"; } private void setMaxFilesize(int sizeInMegaBytes) { PutToS3Applet.maxFileSizeInBytes = PutToS3Applet.maxFileSizeInBytes * sizeInMegaBytes; } } 

Here is the exception:

 Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.util.PropertyPermission user.home read) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374) at java.security.AccessController.checkPermission(AccessController.java:546) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1285) at java.lang.System.getProperty(System.java:650) at javax.swing.filechooser.FileSystemView.getHomeDirectory(FileSystemView.java:393) at javax.swing.plaf.metal.MetalFileChooserUI.installComponents(MetalFileChooserUI.java:253) at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:136) at javax.swing.plaf.metal.MetalFileChooserUI.installUI(MetalFileChooserUI.java:126) at javax.swing.JComponent.setUI(JComponent.java:662) at javax.swing.JFileChooser.updateUI(JFileChooser.java:1763) at javax.swing.JFileChooser.setup(JFileChooser.java:360) at javax.swing.JFileChooser.<init>(JFileChooser.java:333) at javax.swing.JFileChooser.<init>(JFileChooser.java:286) at com.putapplet.PutToS3Applet$ButtonListener.actionPerformed(PutToS3Applet.java:82) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6289) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6054) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4652) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4482) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Component.dispatchEvent(Component.java:4482) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644) at java.awt.EventQueue.access$000(EventQueue.java:85) at java.awt.EventQueue$1.run(EventQueue.java:603) at java.awt.EventQueue$1.run(EventQueue.java:601) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue$2.run(EventQueue.java:617) at java.awt.EventQueue$2.run(EventQueue.java:615) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:614) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
+7
source share
5 answers

Just a note. These are pretty simple remote applets for debugging.

Configure the Java plugin for remote debugging. You do this differently on different platforms, but in windows it will be something similar. Run the control panel, select java. Click around until you find the VM options, and add these VM arguments:

 -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=6789,suspend=n 

6789 will be a debug port.

Starting remote debugging in Eclipse is done using a similar one. Debug Configurations, Remote Java Application, Create a new configuration. For the connection type, select Standard (Socket Attach) and enter locahost as the address and 6789 as the port.

+11
source

Consider turning on the Java console, which allows you to see all the traces of the exception stack, which I hope will make debugging easier for you.

Also, "appletviewer" is designed to test applets outside the browser, including the ability to view exceptions.

+1
source

It is very likely that your SecurityManager does not allow this.

In FireFox, you can check the java console by going to Tools> Java Console. Firefox should already configure it.

+1
source

Implementing a logging solution (e.g. Log4j ) is another option worth considering. A few quick guides on how to do this.

0
source

when you paste your applet into the html page you need to sign the jar applet file See the oracle docs documentation

create your certificate, then use then at the command prompt enter the following command

 jarsigner jarfilename.jar yourcertificatename 
0
source

All Articles