如何使我的用 Java 编写的程序可供其他人使用?

发布于 2024-12-06 05:46:29 字数 296 浏览 0 评论 0原文

所以我写了一个有趣的小程序,我想把它展示给我的一些朋友。我的朋友不是程序员,他们不知道如果我向他们发送包含必要的类和文件的文件夹该怎么办。我希望能够通过电子邮件向他们发送一些内容(或将其放在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凶凌 2024-12-13 05:46:29

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

在 jar 文件中,您将需要(这是必需的)一个 /META-INF 目录,其中包含一个名为 清单 (/META-INF/MANIFEST.MF)。它描述了 jar 文件的“清单”,该文件在某些​​方面是根据运输清单建模的。

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

Main-Class: org.mystuff.path.Main

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

java -jar myproject.jar

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

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

(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” ”。

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

Main-Class: 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

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

(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)

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

万劫不复 2024-12-13 05:46:29

我想说使用小程序就不需要运行任何命令。 如果您有多个类

  1. ,则创建一个 可执行 jar,如果不想创建 jar 将所有代码移动到一个类中
  2. 然后创建一个扩展 Applet,你可以从这个applet类加载你的程序,甚至更好的移动applet 类的所有内容(不是最好的设计,但在这种情况下可以让您的生活更轻松)
  3. 然后创建一个 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

  1. If you have multiple classes then create a executable jar, If don't want to create jar move all your code into one class
  2. Then create a class that extends Applet, You can load your program from this applet class or even better move every thing to the applet class (not the best design, but makes your life easier in this case)
  3. Then create a html file some thing like this, This html has script that will direct what to do to view the program

     <script>
      var jEnabled = navigator.javaEnabled();
      if (!jEnabled){
    
               document.write("<a href=\"http://www.java.com/en/download/help/enable_browser.xml\">Enable Java</a> on your  browser to view me ");
      }
     </script>
    
     <br><br>
     <applet code="AppletLauncher.class" width=100 height=140/>
    
独自唱情﹋歌 2024-12-13 05:46:29

使用 objectdraw 创建可执行 jar 文件:

通常,objectdraw 与 BlueJ IDE 一起使用,因此这些说明适用于使用 BlueJ 和 objectdraw 库。

  1. 确保您有一个具有 begin() 方法的类。这是您在 BlueJ 中运行的类,现在您想将其转换为可执行的 jar 文件。

  2. 您需要创建一个新类,我们将其称为 Main。它应该看起来像这样:

    公共类主要{
    
        公共静态无效主(字符串[] args){
            ClassWithABeginMethod 游戏 = new ClassWithABeingMethod();
            游戏.开始();
        }
    
    }
    
  3. 现在,在 BlueJ 中,打开“项目”下拉菜单。选择“创建 Jar 文件...”。

  4. 在打开的对话框中:
    确保为主类选项选择了类“Main”。
    在“包括用户库”下,选择“objectdraw.jar”选项。
    按继续。

  5. 打开的新对话框询问您要将包含新 jar 文件的新文件夹保存在哪里。将其命名为您想要的名称。结果将是一个具有您选择的名称的新文件夹,其中包含同名的 jar 文件。

  6. 你已经完成了。使用命令行提示符:“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.

  1. 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.

  2. You need to make a new Class, let's call it Main. It should look like this:

    public class Main{
    
        public static void main(String[] args){
            ClassWithABeginMethod game = new ClassWithABeingMethod();
            game.begin();
        }
    
    }
    
  3. Now, in BlueJ, open the Project dropdown menu. Select "Create Jar File...".

  4. 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.

  5. 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.

  6. You're done. Use the command line prompt: "java -jar jarFileName.jar" to run your new jar file.

感性不性感 2024-12-13 05:46:29

您需要告诉我们您正在使用哪个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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文