24小时热门版块排行榜    

查看: 963  |  回复: 6
本帖产生 1 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

cean

金虫 (职业作家)

屌绳一条

[求助] jdk的demo里java2Demo里的Main有什么特殊的么?

在win下,双击java2Demo.jar就可以执行了。
怎么让自己的程序也这样执行呀?我写的老说main找不到。

先谢了
回复此楼

» 本帖已获得的红花(最新10朵)

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

To do great work, you have to have a pure mind.
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

★ ★
微尘、梦想(金币+2): 谢谢参与应助! 2011-06-28 21:35:02
引用回帖:
Originally posted by cean at 2011-06-28 21:18:23:
谢谢。
你怎么想到改这里呢?
这叫什么?我想再看看文档里怎么介绍的。http://muchong.com/bbs/post.php?action=reply&fid=312&tid=3341485

不是想的哦,看你的问题跟jar有关,去oracle看jar相关文档有讲

http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
CODE:
Setting an Application's Entry Point
If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

Main-Class: classname

The value classname is the name of the class that is your application's entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args).

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

java -jar JAR-name

The main method of the class specified in the Main-Class header is executed.

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
6楼2011-06-28 21:28:57
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 7 个回答

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖


cean(金币+1): 谢谢 2011-06-27 21:24:30
xzhdty(金币+1): 欢迎常来 2011-06-28 08:07:17
main是主函数入口每一个程序必须有且只能有一个main
而且main函数必须是public static的.你看看你的属性对吗
对java不熟悉的,建议用blue J写java,能省不少事儿,上手快
玩多了在选择喜欢的IDE
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2011-06-27 19:24:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cean

金虫 (职业作家)

屌绳一条

在win下,这个demo实际上是被Java Platform SE binary程序打开运行的。

我写的application或者applet,在命令行用java命令运行都没问题,但在文件浏览器里右键点击选open with时,不能被Java Platform SE binary程序打开运行。
To do great work, you have to have a pure mind.
3楼2011-06-27 21:22:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖


xzhdty(金币+1): 谢谢 2011-06-28 08:07:38
cean(金币+4): 正是我想要的结果。 2011-06-28 21:13:43
微尘、梦想(程序强帖+1): 2011-06-28 21:34:36
我测试过了,需要修改MANIFEST.MF内容
自己的class打包成jar
CODE:
jar cf test.jar test.class

然后用压缩软件打开jar,进入META-INF文件夹,打开MANIFEST.MF,在Manifest-Version: 1.0下面添加一行
CODE:
Main-Class: test

这个test是你类的名字,也就是指定入口类名
然后双击会运行,但是由于是控制台程序,看不到结果,
要看结果,进入jar目录,运行
CODE:
java -jar test.jar

============================
已经测试了
test.java代码
CODE:
class test {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}


编译
CODE:
C:\>javac test.java

直接运行
CODE:
C:\>java test
Hello World!

打包
CODE:
C:\>jar cf test.jar test.class

打开jar,修改文件.修改后MANIFEST.MF的内容
CODE:
Manifest-Version: 1.0
Main-Class: test
Created-By: 1.6.0_24 (Sun Microsystems Inc.)

运行结果
CODE:
C:\>java -jar test.jar
Hello World!

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
4楼2011-06-28 04:21:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见