我有一个名为helloworld.jar的JAR文件。 为了运行它,我在命令行窗口中执行以下命令:
java -jar helloworld.jar
这工作得很好,但我如何执行它与双击? 我需要安装什么软件吗?
我有一个名为helloworld.jar的JAR文件。 为了运行它,我在命令行窗口中执行以下命令:
java -jar helloworld.jar
这工作得很好,但我如何执行它与双击? 我需要安装什么软件吗?
当前回答
如果需要通过双击jar文件来运行该jar文件,则必须将其创建为“Runnable jar”。你可以简单地用你的IDE来做。
如果您正在使用eclipse,请遵循以下步骤:
To create a new runnable JAR file in the workbench:
1.From the menu bar's File menu, select Export.
2.Expand the Java node and select Runnable JAR file. Click Next.
3.In the Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
4.In the Export destination field, either type or click Browse to select a location for the JAR file.
5.Select an appropriate library handling strategy.
Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.
更多信息可以在Eclipse帮助页:LINK上找到
其他回答
当在命令提示符中启动可运行的jar文件时,这会导致一个有趣的副作用。
如果你尝试(在命令提示符中):
jarfile.jar parameter
没有乐趣,因为这将被翻译成以下内容(这并不管用):
javaw.exe -jar jarfile.jar parameter
但是,下面的命令是可以工作的:
java.exe -jar jarfile.jar parameter
如果你在文件管理器中修改关联,如上所述:
"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %*
然后你可以输入:
jarfile.jar parameter
在命令提示符中,它现在将工作!
编辑:(然而,当你运行一个基于窗体(非控制台)的Java应用程序时,你会得到一个黑色的控制台窗口,所以这不是一个理想的解决方案)
如果您通过在窗口中双击这些jar文件来运行这些jar文件,则不会传递任何参数,因此您的Java代码需要处理堆栈溢出异常,并在最后包含“按下键”函数,否则窗口将会消失。
为了在windows中传递参数,您必须创建一个jar文件的快捷方式,该快捷方式包括目标行中的参数(右键单击快捷方式并选择属性),您不能以这种方式将参数添加到jar文件图标本身。
这里没有一个统一的解决方案,但是任何其他控制台应用程序都会遇到相同的问题。
有一个windows免费应用程序叫做“bat to exe”,你可以用它从一个。bat文件创建一个exe文件,里面有适当的命令行。您还可以将jar文件嵌入到这个应用程序的exe中,并在它完成运行时清理它,因此这可能是一个更优雅的解决方案。
我正在运行Windows 7 x64,无法使用任何这些修复程序。
这条对我来说还是有用的:
http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi
你可以下载一个包含要运行的.bat文件的存档文件,但请检查实际的javaw.exe!!!!的路径
事实上,我也遇到过这个问题。我通过为我的jar文件创建.bat运行程序来解决这个问题。
代码如下:
class FileHandler{
public static File create_CMD_Rnner(){
int exitCode = -1625348952;
try{
File runner = new File(Main.batName);
PrintWriter printer = new PrintWriter(runner);
printer.println("@echo off");
printer.println("title " + Main.applicationTitle);
printer.println("java -jar " + Main.jarName + " " + Main.startCode );
printer.println("PAUSE");
printer.flush();
printer.close();
return runner;
}catch(Exception e){
System.err.println("Coudln't create a runner bat \n exit code: " + exitCode);
System.exit(exitCode);
return null;
}
}
}
然后在你的主应用程序类中这样做:
public class Main{
static String jarName = "application.jar";
static String applicationTitle = "java Application";
static String startCode = "javaIsTheBest";
static String batName = "_.bat";
public static void main(String args[]) throws Exception{
if(args.length == 0 || !args[0].equals(startCode)) {
Desktop.getDesktop().open(FilesHandler.create_CMD_Rnner());
System.exit(0);
}else{
//just in case you wanted to hide the bat
deleteRunner();
// Congratulations now you are running in a cmd window ... do whatever you want
//......
System.out.println("i Am Running in CMD");
//......
Thread.sleep(84600);
}
}
public static void deleteRunner(){
File batRunner = new File(batName);
if(batRunner.exists()) batRunner.delete();
}
}
请注意
这段代码(我的代码)只适用于jar文件,而不是类文件。 jar文件必须与Main类的字符串“jarName”同名
有一种方法不需要用户在他的PC上做改变。runtime . getrtime .exec()允许我们启动cmd.exe并在其中执行命令。因此,当用户单击.jar文件时,java程序可以在命令提示符下自行运行。
public static void main(String[] args) throws IOException {
if(args.length == 0) {
Process p = Runtime.getRuntime().exec("cmd.exe /c start java -jar " + (new File(NameOfClass.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getAbsolutePath() + " cmd");
} else {
//code to be executed
}
}
首先在cmd(命令提示符)上设置路径:
set path="C:\Program Files\Java\jre6\bin"
然后输入
java -jar yourProgramname.jar