简体   繁体   English

通过双击在命令提示符下运行 .jar 文件

[英]Running a .jar file in a command prompt from double click

I'll start of by saying Im on windows 7.我将首先说我在 Windows 7 上。

I have created a .jar file which executes fine from the command line using the - java -jar myJar.jar approach我创建了一个 .jar 文件,该文件使用 -java -jar myJar.jar 方法从命令行正常执行

But what I'm after is to be able to double click on the jar file and for it to open up the command prompt window and run in the command prompt as if i've just typed the java -jar myJar.jar line.但是我想要的是能够双击 jar 文件并让它打开命令提示符窗口并在命令提示符下运行,就好像我刚刚输入了 java -jar myJar.jar 行一样。

When I double click the jar file I do think it is running because a visual part of the java is appearing, but there is no command prompt window showing my console output.当我双击 jar 文件时,我确实认为它正在运行,因为出现了 java 的可视部分,但是没有命令提示符窗口显示我的控制台输出。

After looking around I've come across people saying that javaw which is what the jar files are associated with don't have a console and that I need to associate jar files with java.exe instead of javaw.exe.环顾四周后,我遇到人们说与 jar 文件相关联的 javaw 没有控制台,我需要将 jar 文件与 java.exe 而不是 javaw.exe 相关联。 I've tried this and it didn't seem to work.我已经尝试过了,但它似乎没有用。

Can anyone help?任何人都可以帮忙吗? A step by step would be nice.一步一步来就好了。

I had the same question and the bat file idea was genius and saved me a lot of time rewriting code.我也有同样的问题,bat 文件的想法是天才,为我节省了大量重写代码的时间。 Thanks!(I would have upvoted,but apparently I don't have enough rep.)谢谢!(我会投票,但显然我没有足够的代表。)

Batch (or Bat) files are super easy to make.批处理(或 Bat)文件非常容易制作。

Just put the java -jar YourFile.jar into notepad (if you're on windows), save as Title.bat , and put into the same folder as your jar.只需将java -jar YourFile.jar放入记事本(如果您在 Windows 上),另存为Title.bat ,然后放入与您的 jar 相同的文件夹中。

presto!快! a program open-able by the general public.公众可以打开的程序。

This is IMHO not possible.恕我直言,这是不可能的。 You could open the console from the application itself, but that is OS-dependent.您可以从应用程序本身打开控制台,但这取决于操作系统。 If you need the console open, you have to run the application from it as you already do.如果您需要打开控制台,您必须像以前一样从它运行应用程序。

If you want to display the command line you have to launch your jar with the command line.如果要显示命令行,则必须使用命令行启动 jar。

java -jar MyJar.jar java -jar MyJar.jar

I would do something like this:我会做这样的事情:

(Tested in Widows XP with JRE 1.6, to support other OSs you should verify the path for each OS and select the appropriate console emulator ( xterm , gnome-terminal ... (check for existance and preference...))) (在带有 JRE 1.6 的 Widows XP 中测试,为了支持其他操作系统,您应该验证每个操作系统的路径并选择适当的控制台模拟器( xtermgnome-terminal ...(检查存在和偏好...))

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);//Adds extra slash (??) didn't know why
        String decodedPath = URLDecoder.decode(path, "UTF-8");
        System.out.println(decodedPath);
        Runtime.getRuntime().exec("cmd /c start java -jar \"" + decodedPath + "\" actual_run");
    }
    else {
        System.out.println("Hello World");
        JOptionPane.showMessageDialog(null, "Hello World");
        System.in.read();
    }
}

Alternatively I suggest creating a bat file with this content :或者,我建议使用以下内容创建一个 bat 文件:

java -jar yourjar.jar

This will launch your jar as well as open the command prompt automatically, all from a simple double click on a bat file.这将启动您的 jar 并自动打开命令提示符,只需双击 bat 文件即可。 (The bat file needs to be in the same folder as your jar file, else you need to specify the path to the jar, not just the jar name) (bat文件需要和你的jar文件在同一个文件夹,否则你需要指定jar的路径,而不仅仅是jar名)

This is the easiest solution for beginners:这是初学者最简单的解决方案:

  1. Open any text editor打开任何文本编辑器
  2. write this two lines:写下这两行:

     java "yourmainclassname" pause
  3. save that file as "name".bat将该文件另存为“名称”.bat

  4. Run it with double click from windows GUI从 Windows GUI 双击运行它

(of course this new created .bat file must be in the same folder as the .class) (当然,这个新创建的 .bat 文件必须与 .class 位于同一文件夹中)

..but there is no command prompt window showing my console output. ..但是没有显示我的控制台输出的命令提示符窗口。

No there wouldn't be a console for an executable Jar.不,不会有一个可执行 Jar 的控制台。 You'll need to put that output in the GUI.您需要将该输出放在 GUI 中。

Check your MANIFEST.MF检查您的 MANIFEST.MF

  1. Extract your "executable" jar file into a folder将“可执行”jar 文件提取到文件夹中
  2. find MANIFEST.MF in META-INF folder在 META-INF 文件夹中找到 MANIFEST.MF
  3. check presence of this field: Main-Class: YourMainClassHere检查此字段是否存在: Main-Class: YourMainClassHere

If this field dissapeared then open your original MANIFEST.txt for this point: Main-Class: YourMainClassHere must end with a new line or carriage return如果此字段消失,则为此打开原始 MANIFEST.txt: Main-Class: YourMainClassHere必须以换行符或回车符结尾

Warning: The text file must end with a new line or carriage return. 警告:文本文件必须以换行符或回车符结尾。 The last line will not be parsed properly if it does not end with a new line or carriage return. 如果最后一行没有以新行或回车结束,则不会正确解析。

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

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