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

发布于 2025-02-07 00:43:16 字数 356 浏览 2 评论 0原文

我将从Windows 7上说IM开始。

我创建了一个.jar文件,该文件使用-java -jar myjar.jar.jar方法从命令行执行正常,

但是我追求的是能够双击JAR文件并为其打开命令提示符窗口并在命令提示符中运行,就好像我刚刚键入Java -jar myjar.jar line一样。

当我双击JAR文件时,我确实认为它正在运行,因为Java的视觉部分正在显示,但是没有命令提示窗口显示我的控制台输出。

环顾四周之后,我遇到有人说Javaw是JAR文件与没有控制台相关联的Javaw,我需要将Jar文件与Java.exe而不是Javaw.exe关联。我已经尝试过,它似乎没有用。

谁能帮忙?一步一步会很好。

I'll start of by saying Im on windows 7.

I have created a .jar file which executes fine from the command line using the - java -jar myJar.jar approach

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.

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.

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. I've tried this and it didn't seem to work.

Can anyone help? A step by step would be nice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

虚拟世界 2025-02-14 00:43:16

我也有同样的问题,蝙蝠文件的想法是天才,并节省了很多时间重写代码。 !

谢谢

只需将java -jar yourfile.jar放入记事本(如果您在Windows上),将其另存为title.bat.bat,然后放入与JAR的同一文件夹中。

普雷斯托!公众开放的计划。

I had the same question and the bat file idea was genius and saved me a lot of time rewriting code. Thanks!(I would have upvoted,but apparently I don't have enough rep.)

Batch (or Bat) files are super easy to make.

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.

presto! a program open-able by the general public.

夏天碎花小短裙 2025-02-14 00:43:16

这是不可能的。您可以从应用程序本身打开控制台是OS依赖的。如果需要打开控制台,则必须像已经这样做一样从中运行该应用程序。

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.

月朦胧 2025-02-14 00:43:16

如果要显示命令行,则必须使用命令行启动JAR。

Java -jar myjar.jar

If you want to display the command line you have to launch your jar with the command line.

java -jar MyJar.jar

ぽ尐不点ル 2025-02-14 00:43:16

我会做类似的事情:(

在使用JRE 1.6的Widows XP中进行测试,为了支持其他OSS,您应该验证每个OS的路径,然后选择适当的控制台仿真器(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();
    }
}

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...)))

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();
    }
}
眼波传意 2025-02-14 00:43:16

另外,我建议使用此内容创建一个BAT文件:

java -jar yourjar.jar

这将启动您的JAR,并自动打开命令提示符,所有这些都可以从简单的双击BAT文件中。
(BAT文件需要与JAR文件相同的文件夹,否则您需要指定通往罐子的路径,而不仅仅是JAR名称)

Alternatively I suggest creating a bat file with this content :

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.
(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)

请帮我爱他 2025-02-14 00:43:16

对于初学者来说,这是最简单的解决方案:

  1. 打开任何文本编辑器
  2. 写这两行:

      java“ yourmainClassName”
      暂停
     
  3. 将该文件另存为“ name” .bat

  4. 用Windows GUI的双击

(当然,这是这样)新创建的.bat文件必须与.class同一文件夹)

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

  4. Run it with double click from windows GUI

(of course this new created .bat file must be in the same folder as the .class)

深海不蓝 2025-02-14 00:43:16

..但是没有命令提示窗口显示我的控制台输出。

没有可执行罐的控制台。您需要将该输出放入GUI中。

..but there is no command prompt window showing my console output.

No there wouldn't be a console for an executable Jar. You'll need to put that output in the GUI.

初熏 2025-02-14 00:43:16

检查您

  1. 清单

  2. 主类:yourmainClasshere

如果该字段消失了,则打开您的原始清单。
Main-Class:YourMainClasshere 必须以新的线或马车返回

警告:文本文件必须以新的线路或运输返回结尾。如果不以新的线或运输返回结束,则最后一行将无法正确解析。

Check your MANIFEST.MF

  1. Extract your "executable" jar file into a folder
  2. find MANIFEST.MF in META-INF folder
  3. check presence of this field:
    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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文