简体   繁体   English

有没有办法从 html href URI 启动特定的 java class ?

[英]Is there a way to start a specific java class from html href URI?

The scenario is: I'm trying to creating web pages accessing PhET offline simulations.场景是:我正在尝试创建访问PhET离线模拟的 web 页面。 They have HTML5 simulations which are great and no problems to run in my local machine using a browser;他们有 HTML5 模拟,使用浏览器在我的本地机器上运行非常好,没有问题; and java simulations which require java.和 java 模拟,需要 java。 Related Java simulations are packaged in a jar files.相关的 Java 仿真打包在 jar 文件中。 For example, four simulations of nuclear physics are packaged in a jar file named "nuclear-physics_all.jar".例如,四个核物理模拟打包在名为“nuclear-physics_all.jar”的 jar 文件中。

Extracting this jar file, I can see a file "jar-launcher.properties" which defines what I can choose if I directly run java -jar nuclear-physics_all.jar .提取这个 jar 文件,我可以看到一个文件“jar-launcher.properties”,它定义了如果我直接运行java -jar nuclear-physics_all.jar可以选择什么The file content is:文件内容为:

$ more jar-launcher.properties 
#created by edu.colorado.phet.buildtools.java.JavaBuildCommand
#Fri Aug 09 20:00:55 MDT 2013
project.name=nuclear-physics
project.flavor.alpha-decay.title=Alpha Decay
project.flavor.radioactive-dating-game.mainclass=edu.colorado.phet.nuclearphysics.RadioactiveDatingGameApplication
project.flavor.beta-decay.args=
project.flavor.radioactive-dating-game.title=Radioactive Dating Game
project.flavor.nuclear-fission.args=
project.flavor.nuclear-fission.title=Nuclear Fission
project.flavor.beta-decay.title=Beta Decay
project.flavor.alpha-decay.args=
project.flavor.radioactive-dating-game.args=
project.flavor.nuclear-fission.mainclass=edu.colorado.phet.nuclearphysics.NuclearFissionApplication
project.flavor.beta-decay.mainclass=edu.colorado.phet.nuclearphysics.BetaDecayApplication
project.flavor.alpha-decay.mainclass=edu.colorado.phet.nuclearphysics.AlphaDecayApplication

Using command line I can launch one of the simulation (eg, "Alpha Decay") in this jar file instead of the chooser: java -cp./nuclear-physics_all.jar edu.colorado.phet.nuclearphysics.AlphaDecayApplication Using command line I can launch one of the simulation (eg, "Alpha Decay") in this jar file instead of the chooser: java -cp./nuclear-physics_all.jar edu.colorado.phet.nuclearphysics.AlphaDecayApplication

Here is my question:这是我的问题:
In the simulation list these four simulations (Alpha Decay, Beta Decay, Nuclear Fission, Radioactive Dating Game) are listed separately and I wish to launch exactly the one user clicks.在模拟列表中,这四个模拟(阿尔法衰减、贝塔衰减、核裂变、放射性约会游戏)分别列出,我希望完全启动一个用户点击。 However in web page (offline version, so the uri will be started with file://) I can only use href="file:///<path>/nuclear-physics_all.jar" which will launch the chooser not the simulation I want.但是在 web 页面(离线版本,因此 uri 将以 file:// 开头)我只能使用href="file:///<path>/nuclear-physics_all.jar"这将启动选择器而不是模拟我想。 Is there a way to launch exactly the one simulation (class) with file URI?有没有办法用文件 URI 启动一个模拟(类)?

One hack'ish solution: obviously, you are able in your setup to run a JAR file, just without parameters.一种 hack'ish 解决方案:显然,您可以在设置中运行JAR 文件,而无需参数。 That JAR file is configured to invoke a specific functionality.该 JAR 文件配置为调用特定功能。

So:所以:

  • extract that JAR file提取 JAR 文件
  • set a different Main class in the manifest, see for details在 manifest 中设置不同的 Main class, 详见
  • build a new JAR file from that modified content从修改后的内容构建一个的 JAR 文件
  • repeat重复

In other words: the trick is that you can "run" a JAR file because you specify which class it should invoke.换句话说:诀窍在于您可以“运行” JAR 文件,因为您指定了它应该调用的 class。 When you know which main classes you want, you can duplicate the JAR, just with different Main classes to invoke.当您知道您想要哪些主类时,您可以复制 JAR,只需调用不同的主类即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM