24小时热门版块排行榜    

查看: 1018  |  回复: 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

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖


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的回帖

cean

金虫 (职业作家)

屌绳一条

★ ★
dubo(金币+1): 欢迎常来程序语言版讨论 2011-08-21 14:06:40
dubo(金币+1): 欢迎新虫 2011-08-21 14:06:47
谢谢。
你怎么想到改这里呢?
这叫什么?我想再看看文档里怎么介绍的。http://muchong.com/bbs/post.php?action=reply&fid=312&tid=3341485
To do great work, you have to have a pure mind.
5楼2011-06-28 21:18:23
已阅   回复此楼   关注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的回帖

cean

金虫 (职业作家)

屌绳一条

送鲜花一朵
好像只有jar可以这么执行。不能用Java Platform SE binary程序打开一个class文件。


真是太谢谢了。
To do great work, you have to have a pure mind.
7楼2011-06-28 21:35:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 cean 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿华中科技大学,080502,354分求调剂 +4 守候夕阳CF 2026-03-18 4/200 2026-03-18 22:16 by li123456789.
[考研] 070303一志愿西北大学学硕310找调剂 +6 d如愿上岸 2026-03-12 9/450 2026-03-18 19:50 by macy2011
[考研] 0703化学 305求调剂 +3 FY_yy 2026-03-14 3/150 2026-03-18 19:40 by macy2011
[考研] 0817 化学工程 299分求调剂 有科研经历 有二区文章 +7 rare12345 2026-03-18 7/350 2026-03-18 14:31 by laoshidan
[考研] 304求调剂 +12 小熊joy 2026-03-14 13/650 2026-03-18 12:34 by Linda Hu
[考研] 0703化学336分求调剂 +6 zbzihdhd 2026-03-15 7/350 2026-03-18 09:53 by zhukairuo
[考研] 293求调剂 +11 zjl的号 2026-03-16 16/800 2026-03-18 08:10 by zhukairuo
[考研] 268求调剂 +7 好运连绵不绝 2026-03-12 8/400 2026-03-17 20:28 by xilongliang
[考研] 277调剂 +5 自由煎饼果子 2026-03-16 6/300 2026-03-17 19:26 by 李leezz
[考研] 301求调剂 +4 A_JiXing 2026-03-16 4/200 2026-03-17 17:32 by ruiyingmiao
[考研] 材料专硕326求调剂 +6 墨煜姒莘 2026-03-15 7/350 2026-03-17 17:10 by ruiyingmiao
[考研] 085601求调剂 +4 Du.11 2026-03-16 4/200 2026-03-17 17:08 by ruiyingmiao
[考研] 085600材料与化工求调剂 +5 绪幸与子 2026-03-17 5/250 2026-03-17 16:40 by laoshidan
[考研] 26考研求调剂 +6 丶宏Sir 2026-03-13 6/300 2026-03-17 16:13 by 醉在风里
[考研] 材料与化工专硕调剂 +5 heming3743 2026-03-16 5/250 2026-03-17 14:03 by 勇敢太监王公公
[考研] 085600调剂 +5 漾漾123sun 2026-03-12 6/300 2026-03-16 15:58 by 漾漾123sun
[考研] 本科南京大学一志愿川大药学327 +3 麦田耕者 2026-03-14 3/150 2026-03-14 20:04 by 外星文明
[考研] 289求调剂 +4 这么名字咋样 2026-03-14 6/300 2026-03-14 18:58 by userper
[考研] 304求调剂 +6 Mochaaaa 2026-03-12 7/350 2026-03-13 22:18 by 星空星月
[考研] 328化工专硕求调剂 +4 。,。,。,。i 2026-03-12 4/200 2026-03-13 14:44 by JourneyLucky
信息提示
请填处理意见