I have done a few Java programs but this is the first one that I'm trying to run as an applet so I might have some basic error.
I compiled all the classes and put them together in a jar file called final.
I followed a few tutorials to make a JNLP file that I called jnlp (yeah, I know, I'm very original:) and on which I called to my jar file and I called the JNLP file from an HTML file.
Those are the last lines of the java console output: (before them, the console is filled with my JNLP file)
at sun.plugin2.applet.JNLP2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
preloader: Added pending event 2: ErrorEvent[url=null label=JNLP not an applet, nor a JavaFX application cause=JNLP not an applet, nor a JavaFX application
Here is the JNLP file:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="jnlp.jnlp">
<information>
<title>Encryption Software</title>
<vendor>Atlantis Atlantis</vendor>
<icon href="encrypt_logo.jpg"/>
<offline-allowed/>
</information>
<resources>
<!-- Application Resources -->
<j2se version="1.6+" href=
"http://java.sun.com/products/autodl/j2se"/>
<jar href="final.jar"
main="true" />
</resources>
<application-desc
name="Encryption Software"
main-class="EncryptApplication"
width="500"
height="300">
</application-desc>
<update check="background"/>
</jnlp>
Here is the JS used to launch the applet:
<script src="https://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {code:'', width:500, height:500};
var parameters = {jnlp_href: 'jnlp.jnlp'};
deployJava.runApplet(attributes, parameters, '1.6');
</script>
Is the EncryptionApplication really an applet? To be an applet it must extend Applet or JApplet.
If it is not an applet, it cannot be embedded in HTML.
If it is an applet, the JNLP must declare it as such, so:
<application-desc
name="Encryption Software"
main-class="EncryptApplication"
width="500"
height="300">
</application-desc>
Should be:
<applet-desc
name="Encryption Software"
main-class="EncryptApplication"
width="500"
height="300">
</applet-desc>
Tips
Be sure to check the JNLP using JaNeLA.
Avoid applets like you might avoid the plague. They were always a complete PITA and with recent security updates, have only become more so. See Why CS teachers should stop teaching Java applets for my take on the matter.
It is possible to launch an application (e.g. a JFrame based app.) from a link using Java Web Start. They will be subject to the same (very strict) security requirements of applets, but have none of the applet specific problems (see link in previous point for details).
As mentioned by #ElliottFrisch, it is best to include a valid value for the code attribute. There are circumstances in which it can be left out, but I won't get into that right now..
Related
I'm trying to figure out why my jnlp file isn't working. I'm new to all this so I'd like to check if I did everything correctly. I made a project with JavaFX, in which main class is called Main.java, and then created ExecutableJar file myApp.jar. When I double click on this jar, it opens up and everything works perfectly. Then I tried creating a jnlp file with the same name as my jar file, myApp.jnlp. I did it like this:
<jnlp spec="1.0+"
codebase="url of my site here"
href="myApp.jnlp">
<information>
<title>My Title</title>
</information>
<resources>
<j2se version="1.6+"/>
<jar href="myApp.jar" main="true" />
</resources>
<application-desc
name="Application"
main-class="Main.class"
<application-desc>
<update check="background" />
</jnlp>
In an html file I added it like this:
LAUNCH
Since I don't have any certificate for my jar file, I added url to my file into Exception Site List in Java Control Panel/Security. But when I try to run it I get an exception com.sun.deploy.net.FailedDownloadException: Unable to load resource: https://myUrlHere/myApp.jnlp
I have tried many solutions, including granting all permission in java.policy files, but nothing helped. When I tried it on another computer, on which I haven't tried any weird solutions before I got an error: "Could not find main-class Main in myUrlHere/myApp.jar" Am I doing something incorrectly? I tried running it locally, but it was even worse then. I tried to follow this tutorial: https://www.youtube.com/watch?v=BKuCfNNely4, but instead I used my school server.
I've seen similar questions asked on SO before, but all of them have codebase/href values pointing to things like http://localhost:8000/test and file://D:/MyProject/Foo.jnlp instead of supporting a completely arbitrary "run out of the same local directory" behavior. People have mentioned that some combination of empty and/or missing attributes are supposed to work in recent JREs, but I can't find any combination of syntax that will work. (Normally I'd just go look through the launcher source, but can't for javaws.)
Collectively, we have a lot of experience with the Java language, but we haven't needed to use javaws before, and Oracle's documentation is... frustrating. What we've come up with, anonymized to protect the innocent:
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="9+" version="x.y.z" codebase="???" href="test.jnlp">
<information>
<title>Our Stuff Name</title>
<offline-allowed/>
</information>
<resources>
<java version="9+" java-vm-args="-X:foo"/>
<java version="1.8*" java-vm-args="-X:bar"/>
<jar href="OurStuff.jar" main="true"/>
</resources>
<!-- this is apparently required for local operation even if
the JAR has a manifest with the same main() class -->
<application-desc main-class="com.example.OurStuff"/>
<security>
<all-permissions/>
</security>
</jnlp>
The JNLP file is included as JNLP-INF/APPLICATION.JNLP before signing the JAR file. (The signing and verifying go okay, and running the signed JAR by hand using a java .... -jar OurStuff.jar command line works, but of course isn't available to Windows users.)
But we don't have enough experience among us to get the codebase/href attributes right. What we've tried:
Any codebase attribute trying to refer to the current working directory, like "file://." and similar hacks, all fail with parse errors. This makes total sense. We tried that only out of desperation.
No codebase, no href in the <jnlp> element at all: "The field href has an invalid value in the signed launch file: OurStuff.jar"
No codebase, <jnlp href="test.jnlp">: "The field href has an invalid value in the signed launch file: test.jnlp"
It apparently is supposed to be able to handle only having a location URL in one place, but can't handle not having any. Grrrr.
We tried turning on tracing and cranked the trace level to high. Then it gets frustrating, because at the top of the trace file, javaws had this to say:
basic: Running JVMParams: [JVMParameters: isSecure: true, args:]
-> [JVMParameters: isSecure: true, args:]
basic: XMLParser with _source:
<?xml version="1.0" encoding="utf-8"?>
..... our entire JNLP file verbatim including comments ......
basic: Error parsing test.jnlp. Try to parse again with codebase from LAP
java.net.MalformedURLException: no protocol:
at java.base/java.net.URL.<init>(URL.java:627)
.... internals stack trace .....
Okay, fine, it needs a well-formed URL in an href. But later in the same trace, we see this:
temp: returning LaunchDesc from XMLFormat.parse():
<jnlp spec="9+" codebase="file:/C:/building/OurStuff/branchname/" version="x.y.x" href="file:/C:/building/OurStuff/branchname/test.jnlp">
<information>
<title>Our Stuff Name</title>
<vendor></vendor>
<offline-allowed/>
</information>
<security>
<all-permissions/>
</security>
<update check="timeout" policy="always"/>
<resources>
<java version="9+"/>
<java version="1.8*"/>
<jar href="file:/C:/building/OurStuff/branchname/OurStuff.jar" download="eager" main="true"/>
</resources>
<application-desc type="Java" main-class="com.example.OurStuff"/>
</jnlp>
The codebase attribute, both of the href attributes, all of the missing/optional attributes, have all been worked out correctly! Even entire elements we left out in order to get default behavior! We were in the C:\building\OurStuff\branchname directory at the time! Yay! We thought! Except no! After a couple hundred more lines of correctly testing the signed jar's modification time, copying pieces around in its cache behind the scenes, and matching up the <java/> elements with the locally-installed JREs, it decided to ignore everything it had discovered, but instead printed out the original error and exited.
What do we need to put in the JNLP file to tell javaws "go ahead and use the results from 'codebase from LAP'", whatever LAP means? Or is there some other combination of codebase and href attributes that it will simply look in the same directory as the JNLP file, wherever that happens to be?
is it possible to enrich a Swing-based JApplet (running in a web browser) with JavaFX content that works for Java7 clients? I tried the following things, but without success:
a) Swing JApplet: If I try to add a JFXPanel to a JApplet I get java.lang.ClassNotFoundException: javafx.embed.swing.JFXPanel at runtime.
b) Swing JApplet including javafx_version parameter (version 2.2+, see generated APPLET tag when using dtjava.embed() of the deployment toolkit): Adding a JFXPanel to a JApplet results in java.lang.ClassCastException: SwingInterop cannot be cast to javafx.application.Application at runtime.
c) JavaFX Applet (javafx.application.Application): It seems that swing content can only be shown in a new JFrame (as shown in the official Oracle example at http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html) but not in a JavaFX Applet (javafx.application.Application) itself. SwingNode introduced with Java8 would obviously be the way to go but I didn't found a reliable alternative for Java7.
Is there any way to get option a) running by adding the jfxrt.jar to the classpath (as option b) is obviously doing somehow)?
Oracle have a detailed tutorial on JavaFX in Swing Applications for Java 7.
I suggest you follow that tutorial.
Note that packaging jfxrt.jar with the application is not necessary, and is not recommended (for all the reasons you have in your comment).
Just copy and pasting samples from the Oracle tutorial in case their link goes dead.
Using JavaFX Ant Tasks to Package a Swing Application with Integrated JavaFX Content (key is toolkit="swing"):
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath="${javafx.sdk.path}/lib/ant-javafx.jar"/>
<fx:jar destfile="dist-web/ColorfulCircles.jar">
<fx:application refid="myapp"/>
<fileset dir="build/classes/">
<include name="**"/>
</fileset>
</fx:jar>
<fx:deploy width="800" height="600" outdir="dist-web"
outfile="SwingInterop">
<fx:info title="Swing Interop"/>
<!-- Mark application as a Swing app -->
<fx:application id="myapp"
mainClass="swinginterop.SwingInterop"
toolkit="swing"/>
<fx:resources>
<fx:fileset dir="dist-web" includes="SwingInterop.jar"/>
</fx:resources>
</fx:deploy>
If you aren't using the JavaFX packaging tools, you can still use JavaFX, just edit your jnlp file, to set up the resources with the jfx namespace:
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0"
xmlns:jfx="http://javafx.com"
href="SwingAppWithJavaFXContent.jnlp"
...>
....
<resources>
<j2se version="1.7.0_06+"
href="http://java.sun.com/products/autodl/j2se"/>
<jfx:javafx-runtime version="2.1+"
href="http://javadl.sun.com/webapps/download/GetFile/
javafx-latest/windows-i586/javafx2.jnlp"/>
</resources>
...
</jnlp>
And in your html page, you embed the Java deployment toolkit and make it JavaFX aware:
<html>
<head>
<SCRIPT src="http://java.com/js/dtjava.js"></SCRIPT>
<script>
function launchApplication(jnlpfile) {
dtjava.launch(
{ url : jnlpfile },
{
javafx : '2.2+',
toolkit: 'swing'
},
{}
);
return false;
}
</script>
</head>
<body>
<h2>Test page</h2>
<a href='SampleApp.jnlp'
onclick="return launchApplication('SampleApp.jnlp');">Click</a>
to launch test app.
</body>
</html>
I want to show JavaFX content (a JFXPanel) within the area of a JApplet within a browser window.
That is exactly what this answer explains how to do (at least the packaging portion), the JFXPanel javadoc describes the rest.
For an executable example of embedding JavaFX in a Swing Applet, see the JavaFX "SwingInterop" sample code. You can download the SwingInterop sample code from the "JDK 8 Demos and Samples" link off of the Java download page. A Java 7 equivalent would also be available from the Java 7 download archives.
I'm trying to run my JNLP within an HTML page, but the java plugin does not run the JNLP, runs only the Applet.
Here is my code:
<applet width="800" height="500" codebase="http://127.0.0.1:8888/applets/"
code="br.com.app.server.utils.CompatibilityApplet"
archive="CompatibilityApplet.jar">
<param name="jnlp_ref" value="http://127.0.0.1:8888/applets/testehellojws.jnlp">
</applet>
Thanks.
[EDIT]
An example:
http://java.sun.com/javase/ja/6/ea/6u10/plugin2/jnlp/CompatibilityApplet.java
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="6.0+" codebase="http://127.0.0.1:8888/applets/" href="testehellojws.jnlp">
<information>
<title>App Hello</title>
<vendor>My App Jnlp.</vendor>
<homepage href="http://127.0.0.1:8888/Home.html"/>
<description>My App Jnlp</description>
<description kind="short">Appr</description>
<icon href="images/icone.jpg"/>
</information>
<resources>
<j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
<jar href="hello.jar" main="true"/>
</resources>
<application-desc main-class="br.com.app.server.HelloJWS"></application-desc>
</jnlp>
Please edit your question and just let me know it is edited.
OK
Did you miss the part about the documentBase?
I didn't.
I would recommend removing the space in the applet name attribute.
Done
Can you run any other JNLP embedded applets? E.G. the small (sand-boxed) GIFanim applet at my site?
Yes
What info. do you get reported from here?
java.vendor: Sun Microsystems Inc.
java.version: 1.6.0_26
os.name: Windows 7
os.version: 6.1
<application-desc main-class="br.com.app.server.HelloJWS"></application-desc>
That is the descriptor for a Java application (as opposed to an applet). For an applet, use something more like..
<applet-desc main-class="br.com.app.server.HelloJWS"></applet-desc>
Note:
Even that is not a correct descriptor for an applet, which must explicitly state a documentBase, name, width & height. See the applet-desc section of the JNLP File Syntax for more details.
It must (of course) be an applet. It is not possible to 'embed' an application into a web page using this technique.
JNLP and the Java Plug-In (required for both applets and web start) was deprecated and removed from the API in Java 9.
Your jnlp_ref should probably be an absolute URI, e.g. http://127.0.0.1:8888/applets/testehellojws.jnlp
Also there is a stray space at the start of your code value (though this is probably not the cause of your problem.)
Checking on a related post, I decided to test the tag
<OBJECT>
.
I thought that this would not work with JNLP, so we had tested before.
After changing
<APPLET>
to
<OBJECT>
and referencing my jnlp file as a parameter, it worked!
The browser ignores the code and archive parameters and run my JNLP.
thanks.
Try to remove [archive="CompatibilityApplet.jar"]
I've got the following JNLP:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jnlp PUBLIC "-//Sun Microsystems, Inc//DTD JNLP Descriptor 6.0.10//EN" "http://java.sun.com/dtd/JNLP-6.0.10.dtd">
<jnlp spec="6.0.10" version="1.63" codebase="http://foo.example.com/msi" href="Foo.jnlp">
<information>
<title>Foo</title>
<vendor> Foo Systems, Inc.</vendor>
<homepage href="http://Foo.com"/>
<description>Foo Viewer/Editor Application</description>
<icon href="splash.gif" width="425" height="102" kind="splash"/>
<icon href="Foo.gif" width="64" height="64"/>
<offline-allowed/>
<shortcut>
<desktop/>
<menu submenu="Foo Systems, Inc."/>
</shortcut>
<association mime-type="application-x/wlog" extensions="wlog"/>
<association mime-type="application-x/mplot" extensions="mplot"/>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.6+" initial-heap-size="32m" max-heap-size="255m"/>
<jar href="jars_deployment/TimingFramework-1.0.jar"/>
<jar href="jars_deployment/iText-2.1.5.jar"/>
<jar href="jars_deployment/jai_codec.jar"/>
<jar href="Foo.jar"/>
<jar href="jars_deployment/TimingFramework-1.0.jar"/>
<jar href="jars_deployment/iText-2.1.5.jar"/>
<jar href="jars_deployment/jai_codec.jar"/>
<jar href="jars_deployment/jsch-20090402.jar"/>
<property name="apple.laf.useScreenMenuBar" value="true"/>
<property name="apple.awt.graphics.UseQuartz" value="false"/>
<property name="com.apple.mrj.application.apple.menu.about.name" value="Foo"/>
<property name="java.util.logging.config.file" value="/Users/Shared/logging.properties"/>
</resources>
<application-desc main-class="com.prosc.msi.editor.ui.test.Sandbox"/>
</jnlp>
Most everything is working. When I double-click a .wlog file, it opens up my application. However, it doesn't open the correct file. I read somewhere that JNLP was supposed to pass parameters to the main method indicating which file caused the app to be launched, but this is not happening (on OS X 10.6). I get an empty array to my application's main method.
Probably unrelated, my splash screen doesn't work :(
Any pointers on getting this working?
In a bundled application on Mac OS X, you can implement com.apple.eawt.ApplicationListener, as shown in the Mac OS X Reference Library example, OSXAdapter:
The sample also supports document handing from the Finder by implementing the handleOpenFile() method and registering for supported file types in its Info.plist file.
Addendum: com.apple.eawt.ApplicationListener is deprecated; instead consider com.apple.eawt.Application, which provides getApplication().setOpenFileHandler().
About the splash window
I think the app is just missing the right path to your images.
In this case, like you mention the codebase:
<jnlp spec="6.0.10" version="1.63" codebase="http://foo.example.com/msi" href="Foo.jnlp"> and like you said in the image:
<icon href="splash.gif" width="425" height="102" kind="splash"/>
then, your images have to be in http://foo.example.com/msi/splash.gif
You might want be sure images are in that place.... is what I'm thinking could be the cause.
About the file association.
I'm working right now in the same, and it was seeming to be working while adding a Service from the JNLP API SingleInstanceService. This service is used to register the application like a singleton instance. So, any time your application is lauched, it could retreive the parameters which were used to call you app. In this case, you can use it to see the name of the file that was double clicked on.
http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService
In page above you can find an example and a breaf explanation about that service.
The problem that I found with that, is that I could not see the file name the first time that you run the app.
I mean, this service should register the app the first time, and after this time, you will be seeing the parameters used to launch the app. So, with this service if you opened the first time your app through a double click on your associated file, you will miss the parameters, until the next double click on it. Your app now will not open another instance again, just will pass the parameters to the instantiated application.
So, I found another solution for this.
http://www.knowledgesutra.com/discuss/tpclso-implement-single-instance-application-java.om
The boy in the page above, used a jar file from inside the Java installation to see the VM instance running that was invoked to see the parameters of the launch of your app. It has the code and the jar used to do that in this same page.
Now I'm able to see the line that was calling the app, like this:
com.sun.javaws.Main -open C:\\JNLP example\\applet-cartoon\\drawingPack\\drawing_monitor\\ejemplo.ply C:\Users\IsraelAltamira\AppData\Local\Temp\javaws23
where ejemplo.ply is the file extension that I used in the jnlp descriptor.
And well, Im working in OS X 10.6.4 now, and it seems to work, but at this moment the web start is not doing me the file association, and is not doing the shortcuts to the installed app... but maybe this last solution works for both systems (at least it work from my fake hyperlink, like the line above to open the file).