我有一个名为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上找到
其他回答
在windows上运行.jar文件有很多方法。其中一个正在使用命令提示符。
步骤:
打开命令提示符(以管理员身份运行) 现在为根目录写入“cd\”命令 输入“java jar文件名。jar” 注意:你也可以使用任何第三方应用程序,如WinRAR, jarfix等。
创建.bat文件:
start javaw -jar %*
然后选择app default,用这个。bat文件打开。jar。
当启动.jar文件时,它将关闭cmd。
我正在运行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”同名
在Windows XP *中,你只需要2个shell命令:
C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %*
C:\>assoc .jar=myjarfile
显然使用正确的JRE路径和任何你想要的名称,而不是myjarfile。
检查当前设置:
C:\>assoc .jar
C:\>ftype jarfile
这次使用第一个命令返回的值(如果有的话),而不是jarfile。
*未在Windows 7上测试