如何使用Java编写的程序可用于其他人? [英] How do I make my program, written in Java, usable to other people?

查看:217
本文介绍了如何使用Java编写的程序可用于其他人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一个有趣的小程序,我想把它展示给我的一些朋友。我的朋友,不是程序员,如果我发送包含必要类和文件的文件夹,他不知道该怎么办。我希望能够通过电子邮件向他们发送信息(或将其放在CD / thumbdrive上),然后他们可以双击并让它运行程序。我完全不知道如何实现这一目标。我正在上课,我们使用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.

推荐答案

使用 jar 命令构建一个可执行的jar文件。它基本上是一个具有一定保证的ZIP文件。

Use the jar command to build an executable jar file. It's basically a ZIP file with certain guarantees.

在jar文件中,您需要(这是一个需求)/ META-INF目录,其中包含一个名为清单(/META-INF/MANIFEST.MF)。它描述了jar文件的清单,它在某种程度上模拟了运输清单。

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.

在MANIFEST.MF文件中,你需要指令

Inside the MANIFEST.MF file, you need the directive

Main-Class: org.mystuff.path.Main

当jar文件通过命令exectued时,应指示JVM运行org.mystuff.path.Main类

Which should direct the JVM to run the org.mystuff.path.Main class when the jar file is "exectued" via the command

java -jar myproject.jar

请注意JAR文件倾向于以不同的方式处理类路径,因为它们忽略了CLASSPATH环境变量和-classpath命令行选项。如果需要引用其他JAR文件,则需要在MANIFEST.MF文件中添加classpath指令。使用上面的清单链接查看有关嵌入类路径的详细信息。

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.

根据项目的简单性,单个JAR文件可能足以发送给它们;但是,如果您需要更多,您可能会发现自己必须在几个目录中发送一些文件。在这种情况下,将文件(包括JAR文件)和目录放在第二个zip文件中。虽然您可以从多种不同的方式中选择打包项目,但我建议

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

(layout inside the zip file)
/Readme.txt (a text file describing what to do for new comers)
/License.txt (a text file describing how liberal / restrictive you wish to be concerning your authorship rights.
(the license sounds like overkill, but without it nobody can prove they're not breaking the law)
/bin/myprogram.sh (shell script containing "java -jar ../lib/myprogram.jar")
/bin/myprogram.cmd (Windows batch file containing "java -jar ..\lib\myprogram.jar")
/lib/myprogram.jar (the executable jar file containing your compiled code)
/lib/otherjar.jar (other jar files, as necessary)

这样的zip文件结构,然后安装说明解压缩zip文件;将目录更改为bin并运行myprogram.whatever。

With such a zip-file structure, the installation instructions then become "unzip the zip file; change directory to "bin" and run "myprogram.whatever".

这篇关于如何使用Java编写的程序可用于其他人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆