简体   繁体   English

简单Java程序的JavaScript包装器

[英]Javascript wrapper for simple java program

I want to arrange to have a computer simulation with super simple IO (just print statements), than can run in a browser. 我想安排一个计算机模拟,使用超级简单的IO(仅打印语句),而不是在浏览器中运行。 I can program in java, but know very little about Javascript. 我可以用Java编程,但是对Java知之甚少。 I have a feeling (correct me if I'm wrong) that it may be possible to arrange some HTML/Javascript such that it could neatly wrap a separated chunk of pure java - either in a file on its own, or perhaps inside a larger file with a region marked with... 我有一种感觉(如果我错了,请纠正我),可能可以安排一些HTML / Javascript,使其可以整齐地包装单独的纯Java块-可以单独存储在文件中,也可以存储在较大的文件中带有标记为...的区域的文件

** START JAVA HERE ** **从这里开始**

** END JAVA HERE ** **在这里结束**

...such that someone who only knew java could write a java program there and view it in the browser - so long as the IO was restricted to simple print statements. ...这样,只要IO仅限于简单的打印语句,只知道Java的人就可以在那里编写Java程序并在浏览器中查看它。

Is this possible? 这可能吗? Has it been done already? 已经完成了吗?

EDIT: A bit more background... I have written a computer simulation of a real world phenomenon which I want academics to be able to play with and modify. 编辑:多一些背景...我写了一个计算机模拟的现实世界的现象,我希望学者能够玩和修改。 Academics are more likely to know java than javascript - so I want to hide away all the javascript stuff and let the user just see java. 与JavaScript相比,学者更可能了解Java-因此,我想隐藏所有JavaScript内容,让用户只看到Java。

it may be possible to arrange some html/javascript such that it could neatly wrap a separated chunk of pure java 可能可以安排一些html / javascript,以便它可以整齐地包装纯Java的单独块

No. Java is to Javascript as a car is to a carpet. 不会。Java就像Java语言一样,汽车就像地毯一样。 Browsers are not guaranteed co contain a Java runtime no matter how well they support Javascript. 无论浏览器对Javascript的支持程度如何,都不能保证它们包含Java运行时。 They might, but there's about the same chance of Java being supported as of Flash being supported (with Flash winning). 他们可能会这样做,但是支持Java与支持Flash的机会几乎相同(并赢得Flash)。 It's just a plugin. 这只是一个插件。

Unless you wrap the entire Java compiler and interpreter in javascript (compile them to javascript), you can't do this entirely client-side. 除非您将整个Java编译器解释器都用javascript包装(将它们编译为javascript),否则不能完全在客户端执行此操作。 Even then, it would be pretty slow. 即使那样,它仍然会很慢。 What you can do: 可以做什么:

Option 1: 选项1:

  1. Let the user write a chunk of Java code 让用户编写一段Java代码
  2. Send the chunk to the server 将块发送到服务器
  3. Add bunch of Java code around, and store into a file. 添加一堆Java代码,并将其存储到文件中。 Namely: includes, class header, method header, two closing braces 即:包括,类头,方法头,两个右花括号
  4. Invoke the java compiler on the server. 调用服务器上的Java编译器。 This requires you to have a JDK installed on your server. 这要求您在服务器上安装JDK。
  5. Once the class is compiled, pack this into a jar file with bunch of other, static, classes and a manifest file. 编译完类后,将其与其他一系列静态类和清单文件一起打包到jar文件中。
  6. Respond to the client with a URL of this JAR file. 使用此JAR文件的URL响应客户端。
  7. The client creates a new applet or object tag that refers to this JAR, which the server then feeds to the client. 客户端创建一个引用此JAR的新appletobject标记,然后服务器将其馈送到客户端。
  8. If the user has a JRE on his machine and allows the Java plugin and the applet itself to run, you're done. 如果用户的计算机上有一个JRE,并且允许Java插件 applet本身运行,那么您就完成了。

This is the easiest way to do it, but it's heavily sub-optimal (especially with all the security warnings the client has to confirm). 这是最简单的方法,但是它次优(尤其是客户端必须确认的所有安全警告)。 You could do this instead: 您可以改为:

Option 2: 选项2:

  • Feed an entirely static applet to the client 向客户端提供完全静态的小程序
  • Do not pack several more classes with the compiled one 不要在已编译的类中再打包几个类
  • Instead of creating a new applet element at run time, pass the URL to the existing applet 而不是在运行时创建新的applet元素,而是将URL传递给现有的applet
  • The applet then loads the compiled class only , and then restarts the simulation with the new implementation. 然后,小程序加载已编译的类,然后使用新的实现重新启动仿真。

Also: 也:

  • Jars can take up a lots of space (especially if you don't classload), so if the uploaded source code is not saved visibly, you may wish to delete the jars automatically upon serving as well. Jars会占用很多空间(尤其是如果您不进行类加载),因此,如果上传的源代码没有被明显地保存,则您可能还希望在投放时自动删除Jars。

Option 3: 选项3:

You could compile the Java into Javascript, but only a very limited subset of Java would be supported. 您可以将Java编译为Javascript,但是仅支持非常有限的Java子集。

One such compiler is an integral part of the Google Web Toolkit , designed to write entire websites from Java. 一个这样的编译器是Google Web Toolkit的组成部分,该工具包旨在从Java编写整个网站。 The user would not need to have a Java Runtime Environment on his machine, but would need to restrict himself to a subset of Java that is efficiently compilable to Javascript (you'd need to scratch half of java.util, if I remember correctly). 用户不需要在其计算机上安装Java Runtime Environment,而是需要将自己限制为可有效地编译为Javascript的Java子集(如果我没记错的话,您需要擦除Java.util的一半) 。


Bottom line: You can let the user write Java and then let him observe the results, but the heavy lifting of Java compilation should be done by the server. 底线:您可以让用户编写Java,然后让他观察结果,但是Java编译的繁重工作应由服务器来完成。 If the user doesn't have the Java plugin, then he can't write just any Java code and expect it to run. 如果用户没有Java插件,那么他将无法编写任何Java代码并希望它能够运行。 Also, you better know how to code in Java, including classloading and Javasript -> applet communication. 另外,您最好知道如何用Java进行编码,包括类加载和Javasript-> applet通信。

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

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