简体   繁体   English

在 Windows 上运行 JAR 文件

[英]Running JAR file on Windows

I have a JAR file named helloworld.jar .我有一个名为helloworld.jar的 JAR 文件。 In order to run it, I'm executing the following command in a command-line window:为了运行它,我在命令行窗口中执行以下命令:

java -jar helloworld.jar

This works fine, but how do I execute it with double-click instead?这工作正常,但我如何通过双击来执行它? Do I need to install any software?我需要安装任何软件吗?

Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).最简单的方法可能是升级或重新安装 Java 运行时环境 (JRE)。

Or this:或这个:

  • Open the Windows Explorer, from the Tools select 'Folder Options...'打开 Windows 资源管理器,从工具中选择“文件夹选项...”
  • Click the File Types tab, scroll down and select JAR File type.单击文件类型选项卡,向下滚动并选择 JAR 文件类型。
  • Press the Advanced button.按高级按钮。
  • In the Edit File Type dialog box, select open in Actions box and click Edit...在编辑文件类型对话框中,选择在操作中打开并单击编辑...
  • Press the Browse button and navigate to the location the Java interpreter javaw.exe.按浏览按钮并导航到 Java 解释器 javaw.exe 的位置。
  • In the Application used to perform action field, needs to display something similar to C:\\Program Files\\Java\\j2re1.4.2_04\\bin\\javaw.exe" -jar "%1" % (Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.在Application used to perform action字段中,需要显示类似C:\\Program Files\\Java\\j2re1.4.2_04\\bin\\javaw.exe" -jar "%1" % (注:以'javaw开头的部分' 必须完全一样;路径名的另一部分可能会因您使用的 Java 版本而异)然后按 OK 按钮,直到所有对话框都关闭。

Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html从这里被盗: http : //windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html

In Windows Vista or Windows 7, the manual file association editor has been removed.在 Windows Vista 或 Windows 7 中, 手动文件关联编辑器已被删除。

The easiest way is to run Jarfix , a tiny but powerful freeware tool.最简单的方法是运行Jarfix ,这是一个小巧但功能强大的免费软件工具。 Just run it and your Java apps is back... double-clickable again.只需运行它,您的 Java 应用程序就回来了……再次双击。

If you need to distribute your .jar file and make it runnable at other people's Windows computers, you can make a simple .bat file like this in the command prompt:如果您需要分发您的 .jar 文件并使其可在其他人的 Windows 计算机上运行,​​您可以在命令提示符下创建一个简单的 .bat 文件,如下所示:

java -jar MyJavaTool.jar

and place the .bat file in the same directory as your .jar file.并将 .bat 文件放在与 .jar 文件相同的目录中。

If you have a jar file called Example.jar, follow these rules:如果您有一个名为 Example.jar 的 jar 文件,请遵循以下规则:

  1. Open a notepad.exe打开notepad.exe
  2. Write : java -jar Example.jar写: java -jar Example.jar
  3. Save it with the extension .bat使用扩展名.bat保存它
  4. Copy it to the directory which has the .jar file将其复制到包含.jar文件的目录
  5. Double click it to run your .jar file双击它运行你的.jar文件

An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.在命令提示符下启动可运行的 jar 文件时,一个有趣的副作用会导致出现问题。

If you try (in a command prompt):如果您尝试(在命令提示符中):

jarfile.jar parameter

No joy, because this is being translated to the following (which doesn't work):不高兴,因为这被翻译成以下内容(不起作用):

javaw.exe -jar jarfile.jar parameter

However, the following command does work:但是,以下命令确实有效:

java.exe -jar jarfile.jar parameter

If you change the association in file manager as described above to:如果如上所述将文件管理器中的关联更改为:

"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %*

Then you can type:然后你可以输入:

jarfile.jar parameter

in the command prompt and it will now work!在命令提示符下,它现在可以工作了!

EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)编辑:(但是,当您运行基于表单的(非控制台)Java 应用程序时,您会得到一个黑色的控制台窗口,因此这不是理想的解决方案)

If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a "press a key" function at the end or the window will just disappear.如果您通过在 Windows 中双击它们来运行这些 jar 文件,则不会传递任何参数,因此您的 Java 代码需要处理堆栈溢出异常并在最后包含一个“按键”功能,否则窗口将消失。

In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.为了在 Windows 中传递参数,您必须创建一个到 jar 文件的快捷方式,其中包括目标行中的参数(右键单击快捷方式并选择属性),您不能在此向 jar 文件图标本身添加参数道路。

There isn't a single, consistent solution here, but you would have the same problem with any other console application.这里没有单一的、一致的解决方案,但您在使用任何其他控制台应用程序时都会遇到同样的问题。

There is a windows freeware application called "bat to exe" which you can use to create an exe file from a .bat file with the apropriate command line in it.有一个名为“bat to exe”的 Windows 免费软件应用程序,您可以使用它从带有适当命令行的 .bat 文件创建一个 exe 文件。 you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.您也可以将这个jar文件嵌入到这个应用程序的exe中,并在它运行完成后将其清理干净,所以这可能是一个更优雅的解决方案。

First set path on cmd(command prompt):首先在cmd(命令提示符)上设置路径:

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

then type然后输入

java -jar yourProgramname.jar 

In Windows XP * you need just 2 shell commands:在 Windows XP * 中,您只需要 2 个shell命令:

   C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %* 
   C:\>assoc .jar=myjarfile  

obviously using the correct path for the JRE and any name you want instead of myjarfile .显然,使用 JRE 的正确路径和您想要的任何名称,而不是myjarfile

To just check the current settings:只检查当前设置:

   C:\>assoc .jar  
   C:\>ftype jarfile  

this time using the value returned by the first command, if any, instead of jarfile .这次使用第一个命令返回的值(如果有)而不是jarfile

* not tested with Windows 7 *未在 Windows 7 上测试

There is way without requiring user to do changes on his PC.有一种方法不需要用户在他的 PC 上进行更改。 Runtime.getRuntime.exec() allows us to start cmd.exe and execute commands inside of it. Runtime.getRuntime.exec() 允许我们启动 cmd.exe 并在其中执行命令。 So, it's possible for java program to run itself in command prompt when user clicks .jar file.因此,当用户单击 .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
    }
}

In regedit, open HKEY_CLASSES_ROOT\\Applications\\java.exe\\shell\\open\\command在regedit中,打开HKEY_CLASSES_ROOT\\Applications\\java.exe\\shell\\open\\command

Double click on default on the left and add -jar between the java.exe path and the " %1 " argument.双击左侧的 default 并在java.exe路径和“ %1 ”参数之间添加 -jar。

Besides all of the other suggestions, there is one other thing you need to consider.除了所有其他建议之外,您还需要考虑另一件事。 Is your helloworld.jar a console program?你的 helloworld.jar 是一个控制台程序吗? If it is, then I don't believe you'll be able to make it into a double-clickable jar file.如果是这样,那么我不相信您能够将其制作成可双击的 jar 文件。 Console programs use the regular cmd.exe shell window for their input and output.控制台程序使用常规的 cmd.exe shell 窗口进行输入和输出。 Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.通常,jar“启动器”绑定到 javaw.exe,它不会创建命令外壳窗口。

I´m running Windows 7 x64 and was unable to use any of these fixes.我正在运行 Windows 7 x64 并且无法使用任何这些修复程序。

This one worked for me afterall:毕竟这个对我有用:

http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi

There is an archive which you can download containing a .bat file to run, but check the path of the actual javaw.exe!!!!有一个存档,您可以下载包含要运行的 .bat 文件,但请检查实际 javaw.exe 的路径!!!!

You want to check a couple of things;你想检查几件事; if this is your own jar file, make sure you have defined a Main-class in the manifest.如果这是您自己的 jar 文件,请确保您已在清单中定义了 Main-class。 Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.因为我们知道您可以从命令行运行它,所以要做的另一件事是创建一个 Windows 快捷方式,并修改属性(您必须环顾四周,我没有 Windows 机器可以查看),以便它在打开时执行的命令是您提到的 java -jar 命令。

The other thing: if something isn't confused, it should work anyway;另一件事:如果某些东西没有被混淆,它无论如何都应该起作用; check and make sure you have java associated with the .jar extension.检查并确保您有与 .jar 扩展名关联的 java。

Unfortunatelly, it is not so easy as Microsoft has removed advanced file association dialog in recent Windows editions.不幸的是,这并不容易,因为 Microsoft 在最近的 Windows 版本中删除了高级文件关联对话框。 - With newer Windows versions you may only specify the application that is going to be used to open .jar file. - 对于较新的 Windows 版本,您只能指定将用于打开 .jar 文件的应用程序。

Fixing .jar file opening on Windows requires two steps.在 Windows 上修复 .jar 文件打开需要两个步骤。

  1. 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.在那里找到 .jar 扩展名(可执行 JAR 文件),然后选择 Java 作为默认程序来打开这个扩展名。 It will probably be listed as " Java Platform(SE) ".它可能会被列为“ 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.更快的替代方法可能是直接右键单击 .jar 文件,然后单击“更改...”按钮更改相关程序。

  2. Now open the regedit, and open the HKEY_CLASSES_ROOT\\jarfile\\shell\\open\\command key.现在打开 regedit,打开HKEY_CLASSES_ROOT\\jarfile\\shell\\open\\command键。 Luckilly for us, we may specify parameters there for the (Default) value.幸运的是,我们可以在那里为(Default)值指定参数。 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" %*在我的 Windows 系统上它看起来像: C:\\app\\32\\jre7\\bin\\javaw.exe" -jar "%1" %*但在大多数情况下它是以下字符串: C:\\Program Files\\Java\\jre7\\bin\\javaw.exe" -jar "%1" %*

NOTES:笔记:

  • Do not use java.exe there as it will open the shell window.不要在那里使用java.exe ,因为它会打开 shell 窗口。
  • The jarfix tool mentioned in this thread most likely does nothing more than the registry modification for you.该线程中提到的jarfix工具很可能只是为您修改注册表。 I prefer manual registry change method, as that implies that system administrator can "push" the registry change to all workstations in the network.我更喜欢手动注册表更改方法,因为这意味着系统管理员可以将注册表更改“推送”到网络中的所有工作站。

Another way to run jar files with a click/double-click, is to prepend "-jar " to the file's name.通过单击/双击运行 jar 文件的另一种方法是在文件名前添加“-jar”。 For example, you would rename the file MyJar.jar to -jar MyJar.jar .例如,您可以将文件MyJar.jar重命名为-jar MyJar.jar

You must have the .class files associated with java.exe , of course.当然,您必须拥有与java.exe关联的.class文件。 This might not work in all cases, but it has worked most times for me.这可能并非在所有情况下都有效,但对我来说大多数时候都有效。

I had the same problem in Windows 10. I fixed it using righ-click on the " helloworld.jar " and go to properties and click on change button under "Opens with:" and select " Look for another app on this PC ".我在 Windows 10 中遇到了同样的问题。我使用右键单击helloworld.jar ”并转到属性并单击“打开方式:”下的更改按钮并选择“在此 PC 上查找另一个应用程序修复它。 In the " Open with... " dialog box, go to your Java folder location on your PC and open corresponding jdk folder and then open the bin folder and select " javaw.exe " from there.在“打开方式... ”对话框中,转到 PC 上的 Java 文件夹位置并打开相应的 jdk 文件夹,然后打开bin文件夹并从中选择“ javaw.exe ”。 Then next time your " helloworld.jar " will open the normal way.那么下次你的“ helloworld.jar ”会以正常方式打开。

Usual java location example : " C:\\Program Files (x86)\\Java\\jdk1.8.0_111\\bin ".通常的 java 位置示例:“ C:\\Program Files (x86)\\Java\\jdk1.8.0_111\\bin ”。

PreScript : If your prompt appears and disappears immediately, the reason it does so is that your program gets executed and auto shut. PreScript :如果您的提示出现并立即消失,其原因是您的程序被执行并自动关闭。 Try putting a scanner in the end to terminate and it'll keep your prompt waiting for input before terminating.尝试将扫描仪放在最后以终止,它会让您的提示在终止之前等待输入。 (Or use delay maybe) (或者可能使用延迟)

Was in the very same situation, where running .jar from cmd was working fine, but double clicking did nothing.在完全相同的情况下,从 cmd 运行 .jar 工作正常,但双击什么也没做。

Solution: Open any text editor and write the command line: java -jar Example.jar Save the file as a .bat file.解决方案:打开任意文本编辑器,在命令行中编写: java -jar Example.jar 将文件另存为 .bat 文件。 Run this bat file to get the needed output.运行此 bat 文件以获得所需的输出。

Taking it one step forward, you can convert this bat file to exe file using a simple GUI tool like Bat To Exe Converter.向前迈出一步,您可以使用简单的 GUI 工具(如 Bat To Exe Converter)将此 bat 文件转换为 exe 文件。

Now you can share your .jar as a distribution in .exe file which anyone can use just make sure you keep all the files together.现在,您可以将您的 .jar 作为 .exe 文件中的分发共享,任何人都可以使用该文件,只需确保将所有文件保存在一起即可。 (Especially the .jar and .bat file cause .bat is only a cmd prompt)(How it feels logical) (特别是 .jar 和 .bat 文件,因为 .bat 只是一个 cmd 提示)(感觉如何合乎逻辑)

I am fairly new to development and learning a lot.我对开发和学习相当陌生。 Please excuse for any mistakes if committed.如果犯了任何错误,请原谅。 Suggestions are welcome.欢迎提出建议。

Create .bat file:创建.bat文件:

start javaw -jar %*

And choose app default to open .jar with this .bat file.并选择 app default 以使用此.bat文件打开.jar

It will close cmd when start your .jar file.启动.jar文件时它将关闭cmd

Got the same problem, on Windows 10遇到同样的问题,在 Windows 10 上
The solution:解决方案:

  1. Check your JAVA_HOME and JAVA_PATH.检查您的 JAVA_HOME 和 JAVA_PATH。
    https://javatutorial.net/set-java-home-windows-10 https://javatutorial.net/set-java-home-windows-10
  2. Use Jarfix to restore the assiciation between .jar and javaw.exe使用 Jarfix 恢复.jarjavaw.exe之间的关联
    https://johann.loefflmann.net/en/software/jarfix/index.html https://johann.loefflmann.net/en/software/jarfix/index.html

If you use eclipse for making your java files, you can choose to export it as a runnable jar file.如果您使用 eclipse 制作您的 java 文件,您可以选择将其导出为可运行的 jar 文件。 I did this with my programs and I can just click on the jar and it will run just like that.我用我的程序做到了这一点,我只需点击罐子,它就会像那样运行。 This will work on both windows, as well as os x.这将适用于两个 Windows,以及 os x。

Making a start.bat was the only thing that worked for me.制作 start.bat 是唯一对我有用的东西。

open a text document and enter.打开一个文本文档并输入。 java -jar whatever yours is called .jar java -jar不管你叫什么.jar

save as start.bat in the same folder as the .jar file you want to execute.在要执行的 .jar 文件所在的文件夹中另存为 start.bat。 and then run the.然后运行。 bat蝙蝠

If you need to run the jar file by double clicking on it, you have to create it as a " Runnable JAR ".如果您需要通过双击运行 jar 文件,则必须将其创建为“ Runnable JAR ”。 you can do it simply with your IDE.您可以简单地使用您的 IDE 来完成。

If you're using eclipse, follow these steps :如果您使用的是 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.

more information can be found on Eclipse help Page: LINK更多信息可以在 Eclipse 帮助页面上找到: LINK

There are many methods for running .jar file on windows.在 Windows 上运行 .jar 文件的方法有很多。 One of them is using the command prompt.其中之一是使用命令提示符。

Steps :脚步 :

  1. Open command prompt(Run as administrator)打开命令提示符(以管理员身份运行)
  2. Now write "cd\\" command for root directory现在为根目录编写“cd\\”命令
  3. Type "java jar filename.jar" Note: you can also use any third party apps like WinRAR, jarfix, etc.输入“java jar filename.jar” 注意:您也可以使用任何第三方应用程序,如 WinRAR、jarfix 等。

use .bat file:使用.bat文件:

Put your command in a .bat file.将您的命令放在 .bat 文件中。 here, your command will be java -jar path\\yourJarName.jar .在这里,您的命令将是java -jar path\\yourJarName.jar Something like: java -jar C:\\workspace\\myApplication.jar

Save it and double click on bat file to run your jar.保存并双击 bat 文件以运行您的 jar。

Steps:脚步:

1.) search for Java SE Runtime Environment on Google: https://www.google.com/search?q=Java+SE+Runtime+Environment 1.) 在 Google 上搜索 Java SE Runtime Environment: https : //www.google.com/search?q= Java+SE+Runtime+Environment

2.) install the appropriate version onto your computer 2.) 在您的计算机上安装适当的版本

For compiling:用于编译:

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

For running:对于跑步:

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

Actually, I faced this problem too, I got around with it by making a .bat runner for my jar file实际上,我也遇到了这个问题,我通过为我的 jar 文件制作一个 .bat runner 来解决它

here is the code:这是代码:

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;
       }
   }
}



Then in Your Main application class do this:然后在您的主应用程序类中执行以下操作:

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();
    }
}


Please Note that请注意

  1. this code (my code) works only with a jar file, not a class file.此代码(我的代码)仅适用于 jar 文件,而不适用于类文件。

  2. the jar file must have the same name as the String "jarName" is the Main class jar 文件必须与字符串同名,“jarName”是 Main 类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM