简体   繁体   English

如何创建.jar来运行命令行应用程序

[英]how do I create a .jar to run command line application

For ease I'll use hello world... 为方便起见,我将使用hello world ...

public class HelloWorld{

    public static void main (String [] args){
        System.out.println("Hello World");
    }
}

Compiles and runs fine as you'd imagine. 按照您的想象编译并运行正常。 Then made a file Manifest.txt containing: 然后制作了一个文件Manifest.txt,其中包含:

Main-Class: HelloWorld

then ran: 然后跑了:

jar -cfm Hello.jar Manifest.txt *.class

However when I double click the resulting Hello.jar, nothing happens. 但是,当我双击生成的Hello.jar时,没有任何反应。 I'd expect the console to pop up quickly at least... (I in fact have a more elaborate program which waits for input) 我希望控制台至少能快速弹出......(事实上我有一个更复杂的程序等待输入)

Any ideas why this is not working? 任何想法为什么这不起作用?

It is starting a process in the task manager (windows) but no cmd openning 它在任务管理器(窗口)中启动一个进程,但没有cmd打开

make sure the Main-Class line in the manifest file is followed by an empty line! 确保清单文件中的Main-Class行之后是空行!
Hard to believe, but the specification mandates that each line of the manifest file, including the last one, be terminated by a newline character. 难以置信,但是规范要求清单文件的每一行(包括最后一行)都以换行符终止。

EDIT: 编辑:
If the problem is just with double clicking, try (assuming Windows): 如果问题只是双击,请尝试(假设Windows):

assoc .jar=jarfile
ftype jarfile="<JRE directory>\bin\javaw.exe" -jar "%1" %*

(use these without argument to check the actual settings) (使用不带参数的这些来检查实际设置)

Your clients will need the JRE to run the Java program - the above settings normally are done by the installation of the JRE. 您的客户端将需要JRE来运行Java程序-上述设置通常是通过安装JRE完成的。

EDIT2: EDIT2:
Use java.exe instead of javaw.exe to see the standard console (if your program does not have a GUI) 使用java.exe而不是javaw.exe来查看标准控制台(如果您的程序没有GUI)

Try executing from command line 尝试从命令行执行

java -jar /path/to/YourJar.jar

Make sure you have Main-Class entry 确保您有Main-Class条目


See Also 也可以看看

The manifest needs to be named MANIFEST.MF and it needs to reside in a top level directory called META-INF. 清单需要命名为MANIFEST.MF,它需要驻留在名为META-INF的顶级目录中。 Then it should work. 然后它应该工作。

You should give JSmooth a try. 您应该尝试JSmooth It takes your jar file and wraps it into a Windows executable. 它将您的jar文件包装到Windows可执行文件中。

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

相关问题 如何为.jar命令行脚本创建别名? - How do I create an alias for a .jar command-line script? 如何通过命令行创建 jar 文件? - How do I create a jar file via the command line? 如何从命令行启动.jar文件GUI? - How do I launch a .jar file GUI from the command line? 如何在Windows 7命令行上执行.jar java程序? - How do I execute .jar java program on Windows 7 command line? 如何从命令行在类路径中指定mysql jar? - How do I specify the mysql jar in the classpath from the command line? 如何从 ubuntu 上的命令行使用 maven 运行 java 应用程序? - How do I run a java application with maven from command line on ubuntu? 如何从命令行运行增量编译的NetBeans应用程序? - How do I run an incrementally-compiled NetBeans application from the command line? 如何使用maven打包和运行具有依赖关系的简单命令行应用程序? - How do I package and run a simple command-line application with dependencies using maven? 如何使用命令行在一个类依赖于同一项目中的另一个类的情况下创建jar?(请不要使用构建工具!) - How do I create a jar using the command line where a class is dependent on another class in the same project?(no build tools please!) 如何创建一个使用命令行参数编译和运行java代码的Makefile? - How do I create a Makefile that will compile and run java code with command line arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM