如何使我的用 Java 编写的程序可供其他人使用?
所以我写了一个有趣的小程序,我想把它展示给我的一些朋友。我的朋友不是程序员,他们不知道如果我向他们发送包含必要的类和文件的文件夹该怎么办。我希望能够通过电子邮件向他们发送一些内容(或将其放在 CD/拇指驱动器上),然后他们可以双击并运行该程序。我完全不知道如何实现这一点。我正在上课,我们使用 Linux 计算机(当我不在课堂上时,我使用 mac),我们必须 javac .java 文件,然后 java“文件名” 才能运行它。我有使用 Mac 和 PC 的朋友,我希望他们能够只需单击该程序即可开始运行...
如果有什么不同的话,该程序是使用对象绘制库编写的。
So I've written a funny little program and I want to show it to some of my friends. My friends, not being programmers, have no idea what to do if I send them the folder containing the necessary classes and files. I want to be able to email them something (or put it on a cd/thumbdrive) that they can then double click and have it run the program. I have absolutely no clue how to make this happen. I'm taking a class and we use linux computers (I use a mac when I'm not in class) and we have to javac the .java files and then java "File name" to make it run. I have friends on Mac's and PC's and I want them to be able to just click the program and have it go....
If it makes a difference the program is written using the object draw library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
jar
命令构建可执行 jar 文件。它基本上是一个具有某些保证的 ZIP 文件。在 jar 文件中,您将需要(这是必需的)一个 /META-INF 目录,其中包含一个名为 清单 (/META-INF/MANIFEST.MF)。它描述了 jar 文件的“清单”,该文件在某些方面是根据运输清单建模的。
在 MANIFEST.MF 文件中,您需要指令
当通过命令“执行”jar 文件时,该指令应指示 JVM 运行 org.mystuff.path.Main 类
请注意,JAR 文件往往以不同方式处理类路径,在感觉它们忽略了 CLASSPATH 环境变量和 -classpath 命令行选项。如果需要引用其他 JAR 文件,则需要在 MANIFEST.MF 文件中添加类路径指令。使用上面的清单链接查看有关嵌入类路径的详细信息。
根据“项目”的简单性,单个 JAR 文件可能足以发送给他们;但是,如果您需要更多,您可能会发现自己必须在几个目录中传送一些文件。在这种情况下,请将文件(包括 JAR 文件)和目录放在第二个 zip 文件中。虽然您可以选择多种不同的方式来“打包”这些项目,但我建议
使用这样的 zip 文件结构,安装说明将变为“解压缩 zip 文件;将目录更改为“bin”并运行“myprogram.whatever” ”。
Use the
jar
command to build an executable jar file. It's basically a ZIP file with certain guarantees.Within the jar file you will need (it's a reqirement) a /META-INF directory with a file in it called the manifest (/META-INF/MANIFEST.MF). It describes the "manifest" of the jar file, which is in some ways modeled off a shipping manifest.
Inside the MANIFEST.MF file, you need the directive
Which should direct the JVM to run the org.mystuff.path.Main class when the jar file is "exectued" via the command
Note that JAR files tend to handle classpaths differently, in the sense that they ignore the CLASSPATH environmental variable, and the -classpath command line options. You need to add a classpath directive within the MANIFEST.MF file if you need to reference other JAR files. Use the manifest link above to see the details concerning embedding a classpath.
Depending on the simplicity of the "project", a single JAR file might be enough to ship to them; however, if you need more than that, you might find yourself having to ship a few files in a few directories. In that case, put the files (including the JAR file) and directories in a second zip file. While you could select from a number of different ways to "package" the items, I recommend
With such a zip-file structure, the installation instructions then become "unzip the zip file; change directory to "bin" and run "myprogram.whatever".
我想说使用小程序就不需要运行任何命令。 如果您有多个类
然后创建一个 html 文件,如下所示,该 html 包含脚本,该脚本将指导如何查看程序
<前><代码> <脚本>
var jEnabled = navigator.javaEnabled();
如果(!j启用){
document.write("在浏览器上启用 Java 以查看我的内容");
}
<小程序代码=“AppletLauncher.class”宽度=100高度=140/>>
I would say use an applet then there is no need for any commands to run. Here is what you can do
Then create a html file some thing like this, This html has script that will direct what to do to view the program
使用 objectdraw 创建可执行 jar 文件:
通常,objectdraw 与 BlueJ IDE 一起使用,因此这些说明适用于使用 BlueJ 和 objectdraw 库。
确保您有一个具有 begin() 方法的类。这是您在 BlueJ 中运行的类,现在您想将其转换为可执行的 jar 文件。
您需要创建一个新类,我们将其称为 Main。它应该看起来像这样:
现在,在 BlueJ 中,打开“项目”下拉菜单。选择“创建 Jar 文件...”。
在打开的对话框中:
确保为主类选项选择了类“Main”。
在“包括用户库”下,选择“objectdraw.jar”选项。
按继续。
打开的新对话框询问您要将包含新 jar 文件的新文件夹保存在哪里。将其命名为您想要的名称。结果将是一个具有您选择的名称的新文件夹,其中包含同名的 jar 文件。
你已经完成了。使用命令行提示符:“java -jar jarFileName.jar”运行新的 jar 文件。
Using objectdraw to create an executable jar file:
Typically, objectdraw is used with the BlueJ IDE so these instructions are for using BlueJ and the objectdraw library.
Make sure you have a Class that has the begin() method. This is the class that you run inside BlueJ and now you want to turn it into an executable jar file.
You need to make a new Class, let's call it Main. It should look like this:
Now, in BlueJ, open the Project dropdown menu. Select "Create Jar File...".
In the dialogue box that opens:
Make sure that the Class "Main" is the one selected for the Main class option.
Under "Include user libraries", select the "objectdraw.jar" option.
Press continue.
The new dialogue box that opens is asking where you want to save a new folder that will contain your new jar file. Name it what you would like. The result will be a new folder with the name you selected that contains a jar file of the same name.
You're done. Use the command line prompt: "java -jar jarFileName.jar" to run your new jar file.
您需要告诉我们您正在使用哪个IDE,但只需将其编译成可执行jar,它们就可以在所有具有您再次编译它们的java版本的系统上运行(因此是多平台的)
You need to tell us which IDE you are using, but just compile it into an executable jar, they work on all systems that have the java version you compile them again (so multiplatform)