i've created a java applet last year for a socket connection from a web application to a local running java server. it worked fine.
since the last java updates (7 r21 i guess), i cannot access the methods within javascript anymore. Right now, i reduced the applet to a test applet (without the doPriviligedAction methods) but even this does not work anymore.
The current code is like
import java.applet.*;
public class socketApplet extends Applet {
public void init() {
System.out.println("Applet initialisiert.");
}
public void start() {
System.out.println("Applet gestartet.");
}
public void paint() {
System.out.println("Applet aktualisiert.");
}
public void stop() {
System.out.println("Applet angehalten.");
}
public void destroy() {
System.out.println("Applet beendet.");
}
public String testApplet() {
System.out.println("Applet getestet.");
return "Yep, I'm the Applet.";
}
}
Before the update i could access methods like testApplet() in javascript like this:
document.socketApplet.testApplet();
The applet is self-signed and embedded with an applet html-tag. It is starting (the java console is opening and prints the debug messages defined in the init, start and paint methods) but i cannot access the testApplet() method. The response in Javascript is "undefined" while the applet exists.
after reading a while (a few days now...) about the new security changes, i've added a manifest.txt with the following content:
Main-Class: socketApplet
Permissions: all-permissions
Codebase: *
Trusted-Library: true
No luck with or without the Trusted-Library attribute.
What do i have to do to enable the access with javascript again?
Edit:
The implementation:
<applet id="socketApplet" width="100" height="100" archive="../../socketApplet.jar" name="socketApplet" code="socketApplet" scriptable="true">
I am testing with the newest versions of Firefox and Safari on an up to date Mac OS X machine.
Edit2:
i am creating and signing the jar like this
Edit3:
Okay, now my jar worked a few times (not in a row), i got
and in the console
But most of the time it does not work. restarting the browser, clearing caches, nothing works. going to test this on another pc now (again).
Edit4:
Okay it is running on a virtual machine with windows xp and java 32bit 7u25 - on my 64bit mac just only 1 out of 30 tries.
Okay i found the source of all evil...
it had nothing to do with the applet. the confusing situation was that it worked in firefox on windows and not in firefox on the mac (same FF versions, same java versions). Safari on my mac didn't work because the plugin was disabled...
So it was a Firefox on mac problem only. I've tested different situations and the applet worked when writing the applet code above into a html page. Before, i've created the applet dynamically (what is necessary in the software):
var applet = document.createElement('applet');
applet.archive = 'socketApplet.jar';
applet.id = 'socketApplet';
applet.name = 'socketApplet';
applet.code = 'socketApplet';
applet.scriptable = 'true';
applet.width = '0';
applet.height = '0';
document.body.appendChild(applet);
It works everywhere but not in Firefox on Mac. So as a workaround i have to embed the applet in an iframe and i have to embed the iframe dynamically. that works...
var mFrame = document.createElement('iframe');
mFrame.id = 'testFrame';
mFrame.height = '200';
mFrame.width = '400';
document.body.appendChild(mFrame);
mFrame.src = 'frame.html'; // contains the applet code
Next i am going to change the html tag to the object and embed tag for IE support. Thanks for your help Andrew Thompson!
I dislike the iframe version because the access to the applet via javascript is more complex, but it seems like there is no other way around.
I am going to submit a Mozilla bug ticket for this.
https://bugzilla.mozilla.org/show_bug.cgi?id=912880
Maybe similar to this one but another situation:
https://bugzilla.mozilla.org/show_bug.cgi?id=872969
Related
I've realized that my code has a problem with setting a program icon (instead of the java one) in different platforms. I had written this code for windows and mac:
private void putIcon() {
URL url = ClassLoader.getSystemResource("resources/icon.png");
String name = System.getProperty("os.name");
if (name.startsWith("Win")) {
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
this.setIconImage(img);
} else {
Application.getApplication().setDockIconImage(new ImageIcon(url).getImage());
}
}
While I was testing it in mac there was not problem, but when I tried it windows I realized that won't work because that class isn't in windows:
import com.apple.eawt.Application;
What could I do to solve this issue? For what I've researched it's not possible to have some kind of "if" in the import section of the code, and if that class is there in windows it won't compile.
Check if the specified class Application can be found by using
Class.forName("com.apple.eawt.Application");
If this method does not throw a ClassNotFoundException, invoke the methods you want by using reflection only. Seems a bit hacky but it should work.
Also make sure that you are not importing the class.
I am trying to run Java applet using Google Chrome browser. Everytime I am getting no class found exception. Here is my code.
HelloWorld.java
package my.first.pack;
import java.applet.Applet;
import java.awt.*;
public class HelloWorld extends Applet {
/**
*
*/
private static final long serialVersionUID = 2741715258812838900L;
public void paint(Graphics g) {
g.drawString("welcome", 150, 150);
}
}
Hello.html
<applet code="my.first.pack.HelloWorld" width="300" height="300">
Sign your applet and all the .jar dependencies with a certificate.
Populate your manifest with all the tags mentioned below (it's in xml because I use maven, you can write in the way you prefer)
<codebase>http://location.of.your.jar/</codebase>
<permissions>all-permissions</permissions>
<Application-Library-Allowable-Codebase>http://location.of.your.jar/</Application-Library-Allowable-Codebase>
<Manifest-Version>1.0</Manifest-Version>
<Implementation-Title>App Name</Implementation-Title>
<Implementation-Version>0.1.0</Implementation-Version>
<Application-Name></Application-Name>
<Created-By>1.8.0_45</Created-By>
<Main-Class>package.YourClass</Main-Class>
<mode>development (or production)</mode>
<url>url of the application</url>
Surround your java method with the doPrivileged
Be sure that your browser has the java plugin enabled
Put your http path of your web app in the java exception list
If your url has _ (underscore/underline) probably it won't be recognized.
Try to move your .jar to the same folder of your html, not using the /applet folder.
Take a look on this post, I was having a similar issue.
Remember, this error saying that 'is not a function' is because your .jar is not loading - or you made something wrong with the js syntax, what I don't think so.
Edit 2: I created a html in the root directory and placed the jar file in the same place, I could then get the applet to run (though I had some issues with self-signed security). This tells me the problem is in the applet code. Any ideas why it wouldn't find the class when if I can remove the codebase attribute it runs correctly?
Edit 1: I have updated the entry point to remove the frame. I also tested with the HelloWorld applet and still received the same error.
I'm fairly new to Java so I'll try to explain my problem as clearly as I can and with plenty of detail. If I miss anything please let me know. I'm also aware that this problem is frequently asked about on here, I've done a fair bit of research and found conflicting responses and nothing that worked.
I have developed a JApplet in eclipse, exported a jar file for the project and am attempting to deploy it on my website. However, when I attempt to view the applet online I get the error: ClassNotFoundException. It is probably also worth mentioning that I am trying to deploy this JApplet through wordpress.
Here is the html code I'm using to deploy:
<applet code = 'gui.ConverterGUI.class'
codebase = 'http://www.myurl.co.uk/Java/'
archive = 'AConverter.jar'
width = 800
height = 600>
<param name="permissions" value="all-permissions" />
</applet>
My applet has a few packages and classes which I think I have set up and exported correctly, but incase that is causing problems, here is my main entry point:
public class ConverterGUI extends JApplet {
// Current program ver.
public static final double VERSION = 0.0;
public void init() {
// Make it look nicer.
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
//JFrame f = new JFrame("Converter GUI");
ResultDisplay resultDisplay = new ResultDisplay();
getContentPane().add(resultDisplay, BorderLayout.CENTER);
getContentPane().add(new InputFields(resultDisplay), BorderLayout.NORTH);
}
}
This is my project layout.
I've exported the jar using Eclipse, I've got the impression that this means it has been signed properly already, but if this is not the case and it is causing issues, I'd appreciate being pointed in the right direction to do this (I do have eclipse set up with the JDK rather than JRE).
The issue was in the html code and the way wordpress was interpreting it. For future reference, I fixed the issue by removing the lines from the html code and using double quotes, as follows:
<applet code = "gui.ConverterGUI.class" codebase="http://www.myurl.co.uk/Java/" archive="AConverter.jar" width=800 height=600><param name="permissions" value="all-permissions" /></applet>
Nonetheless I did get some useful advice here. Thanks all.
I've successfully added a custom URI scheme in info.plist on OS X so my Java 1.7 based application (written in Netbeans) is launched whenever the user enters "myApp:SomeParameter" in their browser:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>My App</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myApp</string>
</array>
</dict>
</array>
I have also successfully added the corresponding registry entry for the application if installed on a Windows machine. The problem is that on the Windows platform I can easily pass on parameters (in the above case I want "SomeParameter" from the entered uri "myApp:SomeParameter"). It is simply passed on to the application main method as regular parameters, but this is not the case on OS X. I have done some research and tried this solution but it requires some Cocoa libraries and causes issues when compiled and run on Windows.
EDIT: I have tried to track down a version of AppleJavaExtensions that contains the com.apple.eawt.Application.setOpenURIHandler() but I've only found version 1.4 where it's missing. Any idea why?
Is there another way to pass parameters from a custom URI scheme to a cross platform Java application running on Mac / OS X?
EDIT 2: Please see accepted answer below, but as a side note: we successfully experimented with a possible workaround using AppleScript as a middle-tier. The script in this article can be simplified to receive the full URL with parameters, and then invoke your Java based application with the query string as normal command line parameters.
It looks like you're on the right track. Your Info.plist looks correct. You don't need to create any custom native Cocoa code, just use setOpenURIHandler(). For example:
public class AppleMenus implements com.apple.eawt.OpenURIHandler {
private MyApp myApp;
public AppleMenus(MyApp myApp) {
this.myApp = myApp;
final com.apple.eawt.Application app = com.apple.eawt.Application.getApplication();
app.setOpenURIHandler(this);
}
#Override
public void openURI(final com.apple.eawt.AppEvent.OpenURIEvent oue) {
myApp.openCustomURL(oue.getURI());
}
}
The only reason you would need AppleJavaExtensions is if you are trying to compile this code on a non-Apple environment, such as Windows. Windows won't know what OpenURIHandler is, so you will get a compile error there. AppleJavaExtensions just provides that necessary API without implementation, just for the purposes of being able to compile in these other environments.
Here, is the official latest (and probably last) version: https://developer.apple.com/legacy/library/samplecode/AppleJavaExtensions/Introduction/Intro.html
Note that your URI handler will be called in the currently running instance of you app or will first create a new instance of your app then get called. The OpenURIEvent will contain the entire URI message you send.
The following (for Java 9) will work cross platform (Windows and macOS has been tested):
import java.awt.*;
import java.awt.desktop.OpenURIEvent;
import java.awt.desktop.OpenURIHandler;
import java.net.URI;
import java.net.URISyntaxException;
public class UriLaunchedApp {
public static void main(String[] args) throws URISyntaxException {
try {
Desktop.getDesktop().setOpenURIHandler(new OpenURIHandler() {
#Override
public void openURI(OpenURIEvent e) {
System.out.println("We are maybe on macOS...");
processUri(e.getURI());
}
});
} catch (UnsupportedOperationException e) {
System.out.println("We are maybe on Windows...");
processUri(new URI(args[0]));
}
}
private static void processUri(URI uri) {
System.out.println("Do something with " + uri);
}
}
See too https://docs.oracle.com/javase/9/docs/api/java/awt/Desktop.html#setOpenURIHandler-java.awt.desktop.OpenURIHandler-.
I'm trying to run a code on lifecycle of applet as shown. This file is saved as Lifecycle.java
I compiled it by
javac Lifecycle.java
then tried to run it by
appletviewer Lifecycle.java
package APPLETS;
import java.applet.Applet;
public class Lifecycle extends Applet
{
/*
< APPLET
code = "Lifecycle.class"
height = "300"
width = "300">
< \APPLET>
*/
public void init()
{System.out.print("INIT");}
public void stop()
{System.out.print("STOP");}
public void start()
{System.out.print("Start");}
public void destroy()
{System.out.print("Destroy");}
}
APPLET is not loading then, though my code compiles successfully, no instructions are seen on command prompt. I'm just seeing a blank page with error -> Start:applet not initialized
HERE is the Lifecycle.html code-->
and here is the ERROR-
load: class APPLETS.Lifecycle.class not found.
java.lang.ClassNotFoundException: APPLETS.Lifecycle.class
The appletviewer is expecting to find HTML content so cannot parse the input file. Use appletviewer against a URL rather than a Java source file.
appletviewer is used to view applets using a URL. This URL can be in the format of a local or remote HTML document. Create a HTML document including the tag specifying your class and run the appletviewer against it.
life.html:
<APPLET CODE="APPLETS.Lifecycle" width="300" height="300"></APPLET>
then use
appletviewer life.html
The simplest folder structure for this to run is
./
|life.html
|-APPLETS
Lifecycle.class
Related: The Java Applet Viewer
Aside: Consider using the more up-to-date Swing JApplet.
Put Lifecycle.java in a folder called APPLETS, and try running:
appletviewer APPLETS.Lifecycle