How to call a Java program from a hyperlink locally? - java

I have a html file, there are links in it, I wonder if there is a way to use those links to call a java program to generate another html file ?
Something like this :
<Html>
<body>
Some text
<A Href=[somehow point to a java .class file to run]>My Link</A>
More text
</Body>
</Html>
I know how to use Java to generate html, what I'm asking here is how to pass a parameter to this local java class so that it can generate html file with the input ?
So if the Java program is called : MyHtmlGen.java
Then the class will be MyHtmlGen.class
And then if I run it from the command line, it would be like this :
> java MyHtmlGen my_input
But I don't know how to turn that into the html link above ?

You could use WebStart to launch an Java application from a browser interaction.
The section of the linked documentation titled: "Running a Java Web Start Application From a Browser" provides a demo you can try. The link to the Java application is provided as:
Launch Notepad Application
That documentation states that when you click the link:
Java Web Start software loads and runs the application based on instructions in the JNLP file.
That isn't the behavior I get on Safari 7.1 on OS X 10.9 with Oracle Java 8u40 installed. Instead, I just get the jnlp file downloaded and can double click on the downloaded file to run the application. I think on some browsers, Oracle may provide a plugin to the browser which is able to launch the jnlp referenced application automatically without the user having to also double click on a downloaded jnlp file. Perhaps if the Java deployment toolkit were used, rather than a straight a href link, the user experience might be a bit more seamless.
Note: browser manufacturers have been phasing out support for plugin technology like this, so the experience or even the ability to automatically run the referenced application may vary for both you and your users. Additionally, allowing such plugins to run within a browser environment can increase the security attack vulnerability surface for a user's browser. WebStart is also quick a tricky technology to use and support for your users. So for these reasons I normally don't recommend using WebStart as a deployment solution.

That's just impossible. A link <a> will fire a GET request to the server for the URI set in the href attribute, it's not meant to execute a specific piece of code. If you want to execute code when clicking a link, use JavaScript, but be aware that JavaScript cannot start an instance of JVM and run your exact Java application.
On the other hand, maybe you should look into Applet or JavaFX and embed the java application in your page. Or probably you may submit an action to the server, and at server side you may start the JVM and execute your Java code.

Related

The module hasn't been compiled yet

I have GWT-Maven project created using IntelliJ. I can build and run it but the browser shows a strange error on home page. How to fix it?
I've tried to create new project and import the existing code but it doesn't help.
Project download: GwtStudy
You need to run it GWT Development mode with Jetty.
The you will get a code server at http://127.0.0.1:9876/
and a web server at: http://127.0.0.1:8888/yourapp.html
See also this video: https://www.youtube.com/watch?v=kx9RxrQZnFA
The tutorial is slightly misleading. You need to run the app by choosing "GWT Development mode with Jetty" and not "GWT development mode" as one may believe following the tutorial (the super dev mode is the standard nowadays).
If you run the app using "GWT development mode" you will start only the code server, that will run the java code, but it will not be able to serve the html page that hosts the stockwatcher application.
If you run it with "GWT Development mode with Jetty" you will get a code server at http://127.0.0.1:9876/ AND the web server at: http://127.0.0.1:8888/stockwatcher.html, that is the URL that you need to open with your browser to see the page that hosts the app (you can see that the docs shows the browser opening the url at port 8888 and not at port 9876).
The answer of Jankos is right but for me it was not enough clear. The video is not strictly needed or related to solve the issue, so I added this answer to help gwt beginners like me.
This is the CodeServer, which compiles your app on-demand, and serves the compiled scripts and their source maps.
You need another web server to serve your webapp, including most importantly an html page that includes the nocache.js script.
Depending on how you "run" your app this may or may not already be the case (you'd need to give more information).

Browser Compatibility Issues With Java

Java Compatibility
I have been having some trouble with making a custom browser I am using a default class provided by oracle for a custom browser. One thing I noticed with the browser is that it cannot run java applets without some sort of variation of java browser plugins. How exactly do normal browsers receive information from a web server to run a java program externally? Is there any way to somehow point the browser toward the jre to run the app like on a normal browser. Just need somewhere to start. FireFox apparently references some sort of MIME format under the npjp2.dll native found in the jre directory. Thanks Very Much.
Oracle Example
You can provide the user with a standard html download page in order for him to download a JNLP file which is the standard file to start a java web start app (applet). If the user's system has java installed, it must recognize the .jnlp file and assosiate it as a java app. It's an extra step, for the user to manually download the file instead of running automatically on a web browser, however because of recent security loopholes on java web implementations, most browsers don't trust java anymore, therefore the blocking. If you insist on setting up a web start app in a browser you must make sure that all users have appropriate security configurations on ther java installation (very unlikely).
EDIT:
Here's a very nice tutorial on how to achieve what I just explained.
TUTORIAL

Java Installed vs Java Enabled in browser

Is there a way to detect if java is installed on your machine or if java is disabled on the browser. We use java applets in our application but before the applet is loaded we use "deployJava.js". Sadly even if Java is disabled on the browser it says java is not installed.
So is there way to differentiate if Java is installed or Java is disabled, preferably using javascript. I have also tried using "navigator.javaEnabled()" but it gives me the same result as using "deployJava.js".
You can't.
And that would be very annoying if you could. There's already enough information leaking from the browser to the page, if it started telling people what I have installed but that I have ostensibly disabled, that would be very annoying.
I guess it could be possible and useful to envision an API for that, but in the current state there isn't any: either the plugin is here to be used, or it isn't. It doesn't matter whether it's installed or not.
I guess you'll have to work around it by stating in your user-facing messages that Java either needs to be installed or enabled. Or you could offer to download a diagnostic tool to run it and check it locally, that could be another working alternative, though with the obviously annoying extra-steps to perform (and an additional tool for you to support).
Add a redirect to the HTML page in question. Redirect to javaNotEnabledOrNotInstalled.html (but think of a less descriptive and silly name for it).
Early in the applet init(), call JS to cancel the redirect.
Note also that in the traditional applet element..
<applet
code=..
width=..
height=..
alt='Java is installed but disabled!'>
No Java Plug-In installed in this browser. Get Java free from..
</applet>
In this circumstance, if Java is not installed at all, the user should see..
No Java Plug-In installed in this browser. Get Java free from..
However if Java is installed but disabled, they should see..
Java is installed but disabled!
Here is something js window.navigator.plugin

Running Oracle Forms 11g outside browser

We have an Oracle Forms 11g application which can be started via browser.
This leaves an empty browser window, since Forms opens its own window. However, if you close the empty browser window, the Forms window also get closed.
I like the fact that this kind of installation is a server installation and I can easily update it.
But I would prefer an icon (a shourtcut) on the desktop which just opens the Forms application without the browser window. I hoped to resolve this with the AppletViewer from the Java SDK.
However, when I start it with the URL of the start page I receive:
Warning: <embed> tag requires code attribute.
Exception in thread "main" java.lang.NullPointerException
at sun.applet.AppletViewer.parse(AppletViewer.java:1129)
at sun.applet.AppletViewer.parse(AppletViewer.java:1074)
at sun.applet.Main.run(Main.java:156)
at sun.applet.Main.main(Main.java:98)
Does anyone know how I can open the Forms application directly without the browser (and without a local installation of an app)?
PS: I know Run Oracle Forms as standalone without browser, but it didn't help me, because the referenced link isn't online anymore.
Update 1: the link has been replaced, so this might be a solution I will try.
Update 2: we managed to run our Forms apps via Webstart like any other desktop application. Hurrah!
You can have the the Forms app open in the same browser window it was launched from, just put this in formsweb.cfg:
separateFrame=false
You can checkout the solution using Javascript from this Blog post

Eclipse & Pivot: Launching as an applet

I'm developing an application in Java using Pivot for the GUI elements, in the Eclipse IDE.
Since the end product is supposed to be an applet, I am wondering, if there is a way to launch the application in a browser through Eclipse during the development phase.
I don't want instructions on how to write an html page that loads the libraries and jnlp or something like that, I'm only asking if there is a way to automatically do all that in one-click-mode through Eclipse.
The Run Configurations in Eclipse support launching a Java Applet.
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-launching_java_applet.htm
The Hello World tutorial page on Apache's website gives you the information needed to configure the applet.
http://pivot.apache.org/tutorials/hello-world.html
[Edit in response to comment]
Open the Run Configurations Window and add a new "Java Applet". That's what the first link was meant to indicate you are able to do.
The second link provides a snippet of how you're supposed to run applets using the BrowserApplicationContext, and it's quite specific in the values you provide (and map one for one to the Eclipse launcher values):
<applet code="org.apache.pivot.wtk.BrowserApplicationContext$HostApplet"
archive="lib/pivot-core-[version].jar,lib/pivot-wtk-[version].jar,lib/pivot-wtk-terra-[version].jar,lib/pivot-tutorials-[version].jar"
width="160"
height="80"
>
<param name="application_class_name" value="org.apache.pivot.tutorials.HelloJava"> </applet>
The Main and Parameters tabs should be the only one you're looking at.

Categories