我有一个名为helloworld.jar的JAR文件。 为了运行它,我在命令行窗口中执行以下命令:

java -jar helloworld.jar

这工作得很好,但我如何执行它与双击? 我需要安装什么软件吗?


当前回答

事实上,我也遇到过这个问题。我通过为我的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”同名

其他回答

首先在cmd(命令提示符)上设置路径:

set path="C:\Program Files\Java\jre6\bin"

然后输入

java -jar yourProgramname.jar 

如果使用eclipse生成java文件,可以选择将其导出为可运行的jar文件。我用我的程序做了这个,我只要点击罐子,它就会像这样运行。这将工作在两个windows,以及os x。

处方:如果你的提示符出现并立即消失,那么它这样做的原因是你的程序被执行并自动关闭。尝试在最后放置一个扫描器来终止,它会让您的提示符在终止之前等待输入。(或者使用延迟)

在完全相同的情况下,从cmd运行.jar工作正常,但双击没有任何作用。

解决方案: 打开任何文本编辑器并编写命令行: java -jar示例 将文件保存为.bat文件。 运行这个bat文件以获得所需的输出。

更进一步,您可以使用简单的GUI工具(如bat to exe Converter)将此bat文件转换为exe文件。

现在你可以把你的.jar文件以。exe文件的形式共享,任何人都可以使用,只要确保你把所有文件放在一起。(特别是.jar和.bat文件,因为.bat只是一个cmd提示符)(感觉如何逻辑)

对于开发和学习,我还是个新手。如有错误,请原谅。欢迎提出建议。

不幸的是,这并不容易,因为微软在最近的Windows版本中删除了高级文件关联对话框。-对于较新的Windows版本,您可以只指定将用于打开.jar文件的应用程序。

修复在Windows上打开.jar文件需要两个步骤。

Open the Control Panel, and chose "Default Programs -> Set Associations". Find .jar extension (Executable JAR file) there, and pick Java as default program to open this extension. It will probably be listed as "Java Platform(SE)". A faster alternative perhaps is straightforward right-click on a .jar file, and then change associated program by clicking on the "Change..." button. Now open the regedit, and open the HKEY_CLASSES_ROOT\jarfile\shell\open\command key. Luckilly for us, we may specify parameters there for the (Default) value. On my Windows system it looks like: C:\app\32\jre7\bin\javaw.exe" -jar "%1" %* but in most cases it is the following string: C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*

注:

不要在那里使用java.exe,因为它会打开shell窗口。 本线程中提到的jarfix工具很可能只是为您修改注册表。我更喜欢手动注册表更改方法,因为这意味着系统管理员可以将注册表更改“推送”到网络中的所有工作站。

事实上,我也遇到过这个问题。我通过为我的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”同名