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

I have created an applet that opens a JFileChooser to select a file on the click of a JButton. It works fine when I run it in Eclipse. When I embed it in an HTML page with the applet tag nothing happens when I click the button.
Any suggestions as to why the JFileChooser does not open in the browser would be appreciated, but my main question is how would I debug this? I haven't been able to find anything on Google about how to add a Java console to Firefox 3.6 or Chrome. Is there a way to get some kind of information as to why the JFileChooser does not open?
Debugging answered in comment below
So the console says there is an access denied exception, which I'm guessing is because I have not "signed" the applet. What should the development process be as far as signing an applet goes? Do I have to sign it with a certificate issued by a valid CA before I can test it in a browser, or is there some simple thing you can do while just testing?
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)

Just a side note. It's rather easy to remote debug applets.
Configure the Java plugin to allow remote debugging. You do it in different ways in different platforms, but it will be something like this on windows. Start the control panel, select java. Click around till you find 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 the debug port.
Starting the remote debugging in Eclipse is done by doing something similar to. Debug configurations, Remote Java Application, Create a new configuration. Select Standard (Socket Attach) as connection type, and enter locahost as address and 6789 as port.

Consider enabling the Java Console which will allow you to see all the exception stack traces hopefully making it easier for you to debug.
Also "appletviewer" is designed for testing applets outside a browser including allowing you to see the exceptions.

It's very likely that your SecurityManager isn't allowing it.
In FireFox, you can check the java console by going to Tools > Java Console. Firefox should already have it set up.

Implementing a logging solution (such as Log4j) is another option that might be worth looking at. Some quick tutorials on how to get this up and running.

when you embed your applet in html page you have to sign the applet jar file
See the documentation
oracle docs
create your certificate then use
then in command prompt type the following command
jarsigner jarfilename.jar yourcertificatename

Related

Add subtitle and basic operation panel in VLCJ # Java

Good day colleagues!
I had several trouble using VLCJ and other Java media API-s.
1) I'd add a simple *.srt file to my EmbeddedMediaPlayerCompononent, bot how is this possible?
2) Also, how can I configurate the VLC lib in an x64 Windows OS?
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),libVlc.class);
This not works well.
3) How could I add basic operation interface to my EmbeddedMediaPlayerCompononent like pause/play button?
Thank you, best regards! :)
My "VideoPlayer" class
package GUI.MediaPlayer;
import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import StreamBean.UIBean;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class VideoPlayer extends JFrame{
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
public VideoPlayer(String videoURL) {
String os = System.getProperty("os.name").toLowerCase();
if(os.startsWith("win")){
String registrytype = System.getProperty("sun.arch.data.model");
System.out.println("a rendszered : " +os+" - " +registrytype+ " bites");
if(registrytype.contains("32")){
//Windows 32 bites verzió
System.out.println("Belépett a 32-be");
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}else if(registrytype.contains("64")){
//Windows 64 bites verzió
System.out.println("Belépett a 64-be");
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files (x86)\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}else{
JOptionPane.showMessageDialog(null, "Kérem előbb telepítse a VLC lejátszót.");
}
}
if(os.startsWith("mac")){
//Mac OSX kiadáshoz
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib/");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
this.setTitle("Aktuális videó");
this.setLayout(new BorderLayout());
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
this.add(mediaPlayerComponent,BorderLayout.CENTER);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
//set the Jframe - this - resolution to the screen resoltuion
new UIBean().setWindowSize(this);
this.setVisible(true);
mediaPlayerComponent.getMediaPlayer().playMedia(videoURL);
}
}
To set an external subtitle file:
mediaPlayerComponent.getMediaPlayer().setSubTitleFile("whatever.srt");
How you add a pause/play button is entirely up to you, it requires standard Swing code that is not particular to vlcj. You add buttons to your user interface, and link those buttons up with the media player by using event listeners. For example, this is one way:
JButton playButton = new JButton("Play/Pause");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
mediaPlayerComponent.getMediaPlayer.pause();
}
});
There are numerous reasons why the native library might not be found, but NativeLibrary.addSearchPath(...) certainly does work. You have to be sure you match the CPU architectures of your JVM and your VLC installation (32-bit JVM requires 32-bit VLC, 64-bit JVM requires 64-bit VLC). In most cases you should just use:
new NativeDiscovery().discover();
There are a whole bunch of step-by-step tutorials at http://capricasoftware.co.uk/#/projects/vlcj/tutorial
Focusing on the "basic operation interface" aspect of your question, note that EmbeddedMediaPlayerComponent extends Panel, an AWT component. Accordingly, the VLCJ overlay example shown here overrides paint(). This related, stand-alone example illustrates hit testing in such context.

Why I can't access to a png image into a folder of my project?

I have a simple Java Swing application that show a login form (a JFrame) that have inside a JPanel with a background immage.
The problem is that if I put the location of the immagage using the absolute path of the immage I have no problem but if I try to use the relative path it go into error because seems that can't find the .png background immage.
This is my code:
package com.test.login;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.Dimension;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu.Separator;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import net.miginfocom.swt.MigLayout;
import org.jdesktop.application.SingleFrameApplication;
public class LoginFrame2 extends SingleFrameApplication {
private static final int FIXED_WIDTH = 550;
private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);
public static void main(String[] args) {
System.out.println("DENTRO: LoginFrame() ---> main()");
launch(LoginFrame2.class, args);
}
#Override
protected void startup() {
// TODO Auto-generated method stub
System.out.println("Inside startup()");
JFrame mainFrame = this.getMainFrame(); // main JFrame that represents the Windows
mainFrame.setTitle("XCloud Login");
mainFrame.setPreferredSize(INITAL_SIZE);
mainFrame.setResizable(false);
Container mainContainer = mainFrame.getContentPane(); // main Container into the main JFrame
// JPanel creation and settings of the MigLayout on it:
// JPanel externalPanel = new JPanel();
JPanelWithBackground externalPanel = null;
try {
externalPanel = new JPanelWithBackground("/resources/logo.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));
externalPanel.add(new JLabel("Username"), "w 50%, wrap");
JTextField userNameTextField = new JTextField(20);
externalPanel.add(userNameTextField, "w 90%, wrap");
externalPanel.add(new JLabel("Password"), "w 50%, wrap");
JTextField pswdTextField = new JTextField(20);
externalPanel.add(pswdTextField, "w 90%, wrap");
JButton loginButton = new JButton("Login");
externalPanel.add(loginButton, "w 25%, wrap");
mainContainer.add(externalPanel);
//mainFrame.add(mainContainer);
show(mainFrame);
}
}
The logo.png file is into the resources folder inside my project (at the same level of the src folder).
The line in which I try to access to this resource is:
externalPanel = new JPanelWithBackground("/resources/logo.png");
and the error is:
DENTRO: LoginFrame() ---> main()
Inside startup()
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
at com.test.login.JPanelWithBackground.<init>(JPanelWithBackground.java:19)
at com.test.login.LoginFrame2.startup(LoginFrame2.java:52)
at org.jdesktop.application.Application$1.run(Application.java:187)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
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)
11-nov-2013 10.49.42 org.jdesktop.application.Application$1 run
GRAVE: Application class com.test.login.LoginFrame2 failed to launch
java.lang.NullPointerException
at com.test.login.LoginFrame2.startup(LoginFrame2.java:58)
at org.jdesktop.application.Application$1.run(Application.java:187)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
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)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class com.test.login.LoginFrame2 failed to launch
at org.jdesktop.application.Application$1.run(Application.java:192)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
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)
Caused by: java.lang.NullPointerException
at com.test.login.LoginFrame2.startup(LoginFrame2.java:58)
at org.jdesktop.application.Application$1.run(Application.java:187)
... 14 more
How can I solve it?
Try it without forward slash. Works fine for me running from NetBeans and Eclipse
"resources/logo.png"
The root of your project is probably even before your "src" directory. Try either setting the path to
"/src/path/to/your/resources/logo.png"
or navigate relatively like this
"../resources/logo.png"
Another way is to set the root directory to a variable.. This might come in handy for later use
private final String _RESOURCE_DIR = "/src/path/to/resources/";
File png = new File(_RESOURCE_DIR + "logo.png");
What's the output folder for your resources folder?
I suppose it's the root of the built application ('bin'-directory, and later the root of the JAR), in that case the resource path would be:
"/logo.png"
Usually (depends on the IDE) you can have multiple source and resource folders, and define the output folders for each of them, as well as filters which content should be copied upon a build. Most generic example:
Source folder: /src, Output folder: /bin, Filter: all but .java (Compiler will create and copy the class files instead)
Resource folder: /rsc, Output folder: /bin, Filter: all files.
Assembly: Content of /bin is packaged into a JAR
Example: resource /rsc/images/icons/foo.png will be copied to /bin/images/icons/foo.png, and it's path at runtime (given 'bin' as root, or when packaged into a JAR) will be /images/icons/foo.png, that's then the path you use to load this resource at runtime.
I think problem in next:
You try to get image like next way new ImageIcon("/resources/logo.png") try to use next code:
getClass().getResource("/resources/logo.png") . I think it helps you.
EDIT: for creating File from it use
URL resource = getClass().getResource("/resources/logo.png");
File f = new File(resource.getFile());
Read more about icons.

Java applet can't find resources in jar if run locally

Problem: Java applet can't load resources located inside its jar when run locally on windows platforms. The same applet can load the resources if it is launched from a web server instead of launched locally or if it's launched locally on a linux system. In all cases the applet is launched using an applet tag.
Steps to reproduce
1) Build applet class code below and create a jar containing the following:
TestApplet.class
iconimg.png
test.html
META-INF folder (standard manifest with one line: "Manifest-Version: 1.0")
Here's a link to the image png file I used:
http://flexibleretirementplanner.com/java/java-test/iconimg.png
The file test.html has one line:
<h1>Text from test.html file</h1>
2) create launch.html in same folder as test.jar as follows:
<html><center><title>Test Applet</title><applet
archive = "test.jar"
code = "TestApplet.class"
name = "Test Applet"
width = "250"
height = "150"
hspace = "0"
vspace = "0"
align = "middle"
mayscript = "true"
></applet></center></html>
3) With test.jar in the same local folder as launch.html, click on launch.html
4) Notice that getResource() calls for imgicon.png and test.html both return null.
5) Upload launch.html and test.jar to a web server and load launch.html and notice that the resources are found.
TestApplet.java
import java.applet.AppletContext;
import java.io.IOException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestApplet extends JApplet {
public TestApplet() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void init() {
JPanel topPanel = new JPanel();
JLabel iconLabel;
URL url = TestApplet.class.getClassLoader().getResource("iconimg.png");
if (url != null)
iconLabel = new JLabel(new ImageIcon(url));
else
iconLabel = new JLabel("getResource(iconimg.png)==null");
topPanel.add(iconLabel);
URL url2;
url2 = TestApplet.class.getClassLoader().getResource("test.html");
if (url2 == null) {
JLabel errorLabel = new JLabel("getResource(test.html) == null");
topPanel.add(errorLabel);
} else {
try {
JEditorPane htmlPane = new JEditorPane(url2);
topPanel.add(htmlPane);
} catch (IOException ioe) {
System.err.println("Error displaying " + url2);
}
}
getContentPane().add(topPanel);
}
private void jbInit() throws Exception { }
}
Oracle has decided to modify the behavior of getDocumentBase(), getCodeBase() and getResource() because security reasons since 1.7.0_25 on Windows: http://www.duckware.com/tech/java-security-clusterfuck.html
It seems there is a lot of discussion about this change because it breaks some important valid and secure use cases.
After further research and discovering that this is a windows-only problem, I'm calling this one answered.
It's almost certainly a java 1.7.0.25 bug. The applet runs fine from a web server and also runs fine locally on a virtual Ubuntu system (using VirtualBox on windows). Hopefully the bug report i submitted will be helpful to the java folks.
Thanks for the responses. Btw, it was Joop's comment about case sensitivity that spurred me to test a linux system just for kicks. Thanks for that!

Getting error while using webrenderer in my applet

I am trying to get this code work (applet) its based on webrenderer trial , I got this code in example section.
Though I tried searching there site but it was of no use.I want to render page on my applet .
Below is full code I used , I really don't know what is wrong there.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.webrenderer.swing.*;
/**
* This example shows how to set up WebRenderer to run Java Applets
*
* #author JadeLiquid Software.
* #see www.webrenderer.com
*/
public class AppletExample
{
// instance of a browser
IMozillaBrowserCanvas browser;
JTextField textfield;
public AppletExample()
{
// Creates a JFrame to host the browser
JFrame frame = new JFrame("Applet Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and append the browser
frame.setContentPane(createContent());
frame.setSize(640, 480);
frame.setVisible(true);
}
public JPanel createContent()
{
JPanel panel = new JPanel(new BorderLayout());
textfield = new JTextField();
panel.add(BorderLayout.NORTH, textfield);
// The action listener triggers when text is entered in the text field
textfield.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
browser.loadURL(textfield.getText());
}
});
// If you are behind a proxy server, it may be necessary to set the proxy authentication
// to enable access for page loading. Enable the following call(s) with the appropriate
// values for domain, port..
// These settings will need to be applied to both the browser and to enable applets.
//int yourProxyPort = 8080;
//String yourProxyServer = "proxyserver";
// Apply proxy settings to enable applets. Username and password will need to entered as
// a dialog does not appear automatically as is the case with the browser proxy.
//BrowserFactory.setAppletProxySettings(yourProxyServer, String.valueOf(yourProxyPort));
//BrowserFactory.setAppletProxyAuthentication("username", "password");
// Create a browser instance
browser = BrowserFactory.spawnMozilla();
// Required if a proxy server is being used.
//browser.setProxyProtocol(new ProxySetting( ProxySetting.PROTOCOL_ALL, yourProxyServer, yourProxyPort));
//browser.enableProxy();
// Required call to allow applets to function
browser.setAppletMode(IMozillaBrowserCanvas.ENABLE_JAVA_APPLETS);
// Load a site with many applet examples
browser.loadURL("java.sun.com/applets/jdk/1.4/index.html");
// Attach to the panel
panel.add(BorderLayout.CENTER, browser.getComponent());
return panel;
}
public static void main(String[] args)
{
/*
* TODO: Insert your username and key in the following setLicenseData call.
* WebRenderer operates with reduced functionality if the username and key are not correct.
* The sample key used below is a limited-duration trial key.
*/
BrowserFactory.setLicenseData( "30dtrial" , "9Q2QFT9JIK4OHO5BSNKHNMM9EMTR5NRG" );
new AppletExample();
}
}
When i try to run it i get error
java.lang.reflect.InvocationTargetException
Here is debug info
Initializing jdb ...
Internal exception:
java.io.IOException: The handle is invalid
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:242)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:273)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at com.sun.tools.example.debug.tty.TTY.<init>(TTY.java:751)
at com.sun.tools.example.debug.tty.TTY.main(TTY.java:1067)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.applet.Main.invokeDebugger(Main.java:314)
at sun.applet.Main.run(Main.java:143)
at sun.applet.Main.main(Main.java:98)
Please make sure that you have followed these steps as outlined in the documentation: http://www.webrenderer.com/products/swing/developer/developersguide/files/appletdeploy.htm

Directory Reader Applet Loads in Eclipse, but not in Browser

I'm fairly new to programming and I just wrote an applet program that is supposed to list the files in a directory. The applet works well in eclipse, yet the issue is when I attempt to run the applet in a browser the GUI loads, yet the applet will not respond as it does in eclipse. Any help would be greatly appreciated. :)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JApplet;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DirReader extends JApplet{
private JTextArea outputWindow;
private JTextField dirPath;
private String path;
private Font font;
private File folder;
private File[] listOfFiles;
public void init(){
font = new Font("Times New Roman", Font.PLAIN, 16);
dirPath = new JTextField("Enter Directory Path");
dirPath.setFont(font);
outputWindow = new JTextArea();
outputWindow.setEditable(false);
outputWindow.setFont(font);
outputWindow.setBackground(Color.DARK_GRAY);
outputWindow.setForeground(Color.ORANGE);
add(dirPath, BorderLayout.NORTH);
add(outputWindow, BorderLayout.CENTER);
setSize(400,750);
dirPath.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event) {
path = dirPath.getText();
folder = new File(path);
listOfFiles = folder.listFiles();
System.out.println("Directory path set");
ListOfFiles();
}
}
);
System.out.println("Progam Intilized:");
}
public void ListOfFiles(){
outputWindow.setText(null);
try{
for(int counter = 0 ; counter < listOfFiles.length ; counter++ ){
if(listOfFiles[counter].isFile()){
outputWindow.append("[FILE] " + listOfFiles[counter].getName()+ "\n");
System.out.println("[FILE] " + listOfFiles[counter].getName());
}
else if(listOfFiles[counter].isDirectory()){
outputWindow.append("[DIR] " + listOfFiles[counter].getName() + "\n");
System.out.println("[DIR] " + listOfFiles[counter].getName());
}
}
}catch(Exception e){
System.out.println("Error: Directory could not be found.");
outputWindow.setText("Error: Could not find directory.");
}
}
}
Of course it does NOT work in browser
Because
when applet run in a browser as a security precaution there is no access to read files on the local file system.
Solution
Make your program as a Desktop application run in desktop NOT Applet run in browser
An untrusted applet cannot get a directory listing
A trusted applet cannot get a directory listing on the server (only the client).
For an applet to gain a file listing on the server, it will need to be explicitly told what the files are. Two common ways are:
Add the file list as parameters for the applet.
Have the applet access server side functionality to get a file listing.
BTW:
}catch(Exception e){
Should become..
}catch(Exception e){
e.printStackTrace(); //...
Be sure to figure out how to access the Java Console to see the output. Debugging applets without that information is like trying to debug wearing a blind-fold.

Categories

Resources