创建一个核心java程序是为了让jar失败? [英] It is posible to create a core java program as to get jar fail?

查看:88
本文介绍了创建一个核心java程序是为了让jar失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个核心java程序(helloworld!)以获取jar文件或exe文件是可行的吗?

解决方案

一个jar文件只不过是一个zip文件,包含已编译的.class文件和应用程序所需的资源(html文件,图片,...)。 jar文件可能还包含一个清单文件,它存储jar文件的一些属性,其中一个属性是Main-Class属性,它告诉java当有人启动你的jar文件时应该执行哪个类(使用java -jar) < jarfile>或者如果.jar扩展名与系统上的java -jar命令相关联,则双击/按住它。



在一个大型程序中,每个类都可以包含一个main函数(我经常使用它来为每个类编写简单的测试),这就是为什么你需要在清单中选择一个类文件作为主类。



一个简单的Hello Worldjar教程。

http://www.herongyang.com/Java-Tools/jar-Create-First-JAR-File.html [ ^ ]

唯一的问题是本教程是在它没有在清单文件中设置Main-Class属性。创建manifest.mf文件并将Main-Class:Hello行写入其中,然后在创建jar文件时通过指定-m manfiest.mf参数来重做教程。如果您的主类在包内(不在默认包中),那么也指定包:Main-Class:package1.package2.Hello



运行你的jar:java -jar hello.jar


关键字是部署 - 如果你想从你的东西创建一个你需要部署它的应用程序。

It is posible to create a core java program(helloworld!) as to get jar file or exe file?

解决方案

A jar file is nothing more than a zip file that contains your compiled .class files and the resources needed by your application (html files, pictures, ...). A jar file might also contain a manifest file that stores some properties for your jar file, one of these properties is the "Main-Class" property that tells java which class should be executed when someone launches your jar file (with "java -jar <jarfile>" or by double-clicking/enter-pressing it if the .jar extension is associated with the "java -jar" command on your system).

In a large program every class can contain a main function (I often used this to write simple tests for each class), this is why you need to select one class file in the manifest as your main class.

A simply "Hello World" jar tutorial.
http://www.herongyang.com/Java-Tools/jar-Create-First-JAR-File.html[^]
The only problem with this tutorial is that it didn't set the "Main-Class" attribute in the manifest file. Create your manifest.mf file and write the "Main-Class: Hello" line into it and then redo the tutorial by specifying the "-m manfiest.mf" parameter too when you create your jar file. If your main class is inside a package (not in the default package) then specify the package as well: "Main-Class: package1.package2.Hello"

Run your jar: "java -jar hello.jar"


the keyword is "deployment" - if you want to create an application from your stuff you need to "deploy" it.


这篇关于创建一个核心java程序是为了让jar失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆