如何在java中创建jar启动器? [英] How can I create a jar launcher in java?

查看:76
本文介绍了如何在java中创建jar启动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建了一个不错的java应用程序。

我现在要做的是创建一个单独的应用程序,它允许你选择一个jar文件,从你的文件系统,然后按一个按钮,然后打开它。



我希望应用程序可以在Mac和PC上运行。



现在我知道Swing应用程序可以单击和打开,但是,无法双击打开控制台应用程序(AFAIK)。



我也知道我可以创建一个打开jar的* .bat(Windows)或* .sh(Mac)文件,但我想更多地练习Java,并想学习如何做到这一点。



谢谢大家,



技嘉巨人

Hello all,
I have created a nice java application.
What I want to do now, is create a separate application, that allows you to select a jar file, from your filesystem, and then press a button, and open it.

I want the application to work on Mac, and PC.

Now I know the ability to click and open is available with Swing applications, however, console applications cannot be double clicked to open (AFAIK).

I also know that I can create a *.bat (Windows) or *.sh (Mac) file that opens to jar, but I want to practice Java some more, and would like to learn how to do this.

Thank you all,

Gigabyte Giant

推荐答案

在Windows上,jre中有两个可执行文件来运行java应用程序:java和javaw。主要的区别是java是一个控制台应用程序而javaw不是,它通常用于执行gui应用程序。如果在Windows中单击一个jar文件,那么通常会启动javaw,因为java安装程序将jar文件与javaw -jar%1命令关联,其中%1是单击的jar文件。



使用控制台运行jar文件:

On windows there are two executables in the jre to run java applications: java and javaw. The main differenc is that java is a console application while javaw isn't, it is usually used to execute gui applications. If you click a jar file in windows then usually javaw is started because the java installer associates jar files with the "javaw -jar %1" command where %1 is the clicked jar file.

Running a jar file with console:
java -jar jarfile


没有控制台的



without console:

javaw -jar jarfile





检测java或javaw程序的位置可以是棘手的,可能一种跨平台的方式是寻找JAVA_HOME env var,但它不一定是设置的。另一方面,一台机器可以安装几个版本的jre,大多数Java开发IDE要求你为你的项目选择其中一个版本。我不知道如何检测所有这些,这可能是平台特定的,如果你对此感兴趣,请参阅谷歌...



如果您的启动程序是也是一个java应用程序,然后你可以通过查看正在运行的启动器应用程序的环境变量找到jre的目录。例如:



Detecting the location of the java or javaw programs can be tricky, probably one crossplatform way is looking for the JAVA_HOME env var but it isn't necessarily set. On the other hand a machine can have several versions of the jre installed, most java development IDEs ask you to select one of them for your project. I don't know how to detect all of them and this can be platform specific, see google if you are interested about this...

If your launcher program is also a java application then you can find out the directory of the jre by looking at the environment variables of your running launcher application. For example:

String jre_dir = System.getProperty("java.home");



然后你可以运行java的控制台版本来自这个jre文件夹的程序来执行所选的jar文件(使用jre_dir / bin / java -jar your_jar命令)。


Then you can run the console version of the java program from this jre folder to execute the selected jar file (with the "jre_dir/bin/java -jar your_jar" command).


这篇关于如何在java中创建jar启动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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