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

java -jar helloworld.jar

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


当前回答

开始。球棒是唯一对我有用的东西。

打开一个文本文档并输入。Java -jar,不管你叫。jar

另存为start.bat,保存在与要执行的.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”同名

如果你需要分发你的.jar文件,让它可以在其他人的Windows电脑上运行, 你可以在命令提示符中创建一个简单的.bat文件:

java -jar MyJavaTool.jar

并将.bat文件放在与.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上找到

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

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

编译:

javac  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart.java

为运行:

java  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart