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

java -jar helloworld.jar

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


当前回答

在regedit中打开HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\命令

双击左边的default,在java.exe路径和“%1”参数之间添加-jar。

其他回答

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

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

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

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

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

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

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

如果需要通过双击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上找到

步骤:

1.)在谷歌上搜索“Java SE Runtime Environment”:https://www.google.com/search?q=Java+SE+Runtime+Environment

2.)在你的电脑上安装合适的版本

在regedit中打开HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\命令

双击左边的default,在java.exe路径和“%1”参数之间添加-jar。