Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 运行没有main函数的Java类
linux_china



版主


发贴: 752
积分: 240
于 2005-11-18 09:46 user profilesend a private message to usersend email to linux_chinasearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
通常我们需要在java类中添加main函数才能在命令行运行这个类,但是有一种方面确可以让你在没有main函数的情况下也可以运行java class。
建立一个新的class,代码如下:

public class NoMainMethod
{
static
{
System.out.println("Hello word, no main method!");
System.exit(0);
}
}


static语句块会在加载类的时候加载,它在main函数之前运行。 当static语句块运行完毕后,会寻找main函数,如果没有找到,就会i抛出异常。但是在以上的代码中,static语句块执行后,我们终止程序的运行,所以异常就不会被抛出啦。



作者 Re:运行没有main函数的Java类 [Re:linux_china]
bluecrystal





发贴: 2788991
积分: 48
于 2005-11-18 10:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
晕死
Big Smile



Just Software & Travel
-- 我的blog -- 技术点滴/经验分享
作者 Re:运行没有main函数的Java类 [Re:linux_china]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-11-18 15:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
To linux_China

Please don't take this code as a good example to run Java programs. It is a bad example and bad code practice. It is especially bad for teaching beginners!

To beginners

Please don't learn this kind of trick, please don't!!!

Thanks all!!!



作者 Re:运行没有main函数的Java类 [Re:linux_china]
langziyu





发贴: 2
积分: 0
于 2005-11-18 15:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
同感,C/C++如果没有主函数的话,不能运行。
这也许是与其他编程语言的区别吧!!!
但不管如何,他的环境变量是必须的吗

一只菜鸟



作者 Re:运行没有main函数的Java类 [Re:linux_china]
bukaoyan





发贴: 43
积分: 0
于 2005-11-18 16:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵
这方法不错!!



作者 Re:运行没有main函数的Java类 [Re:bukaoyan]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-11-18 16:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
bukaoyan wrote:
呵呵
这方法不错!!


No, 这方法 is very very bad!!!!

If you write a real application use such trick, you will get fired by any decent company.

Please don't learn this trick, please don't!!!!!

Thanks!


gongshi edited on 2005-11-18 16:33

作者 Re:运行没有main函数的Java类 [Re:gongshi]
wmgreat



CJSDN高级会员


发贴: 286
积分: 105
于 2005-11-18 20:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
gongshi wrote:
To linux_China

Please don't take this code as a good example to run Java programs. It is a bad example and bad code practice. It is especially bad for teaching beginners!

To beginners

Please don't learn this kind of trick, please don't!!!

Thanks all!!!


gongshi是一个非常严厉的版主。:)

说实话,明白静态变量的初始化过程并不是一个坏事,更深一步,可以深入到类装载。
我想,一个好学的beginner应该不会把这些代码作为猎奇的对象,而是应该研究其所以然。



新世纪的曙光点亮了我们略带近视的眼睛。我们终于明白,诸神早已迎来了他们的黄昏,旧的偶像也都已纷纷落地,新的偶像还在摇篮里嗷嗷待哺……
作者 Re:运行没有main函数的Java类 [Re:wmgreat]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-11-19 01:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
wmgreat wrote:
说实话,明白静态变量的初始化过程并不是一个坏事,更深一步,可以深入到类装载。


Yes, you're correct! It is a hard topic for beginners.

However, the title was not "静态变量的初始化过程". but " 运行没有main函数的Java类 "

That is why it was very bad!!!!


gongshi edited on 2005-11-19 03:58

作者 Re:运行没有main函数的Java类 [Re:linux_china]
Infinite

快乐学习



发贴: 35
积分: 1
于 2005-11-20 02:15 user profilesend a private message to usersend email to Infinitesearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵,好巧妙阿。


作者 Re:运行没有main函数的Java类 [Re:linux_china]
Jcat

熊猫的猫



发贴: 266
积分: 16
于 2005-11-20 14:07 user profilesend a private message to usersend email to Jcatsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不得不佩服师兄的创意~

更不得不佩服大猫的严谨,向你学习!



编程,游泳,睡觉--SleepingCat
作者 Re:运行没有main函数的Java类 [Re:Jcat]
linux_china



版主


发贴: 752
积分: 240
于 2005-11-20 21:07 user profilesend a private message to usersend email to linux_chinasearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
建议初学者不要这么用,这纯粹是技巧,没有任何实际意义.慎用.


作者 Re:运行没有main函数的Java类 [Re:linux_china]
francis36





发贴: 4
积分: 0
于 2005-11-20 22:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我相信这是JAVA独特的一面~~~


作者 Re:运行没有main函数的Java类 [Re:linux_china]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-11-21 00:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
linux_china wrote:
建议初学者不要这么用,这纯粹是技巧,没有任何实际意义.慎用.


Thanks, linux_china, my dear friend !!!!

Actually, we can write a total C-like code by only using static. I used to work in a telecom company, their all C++ code was not OO at all, just C code compiled in the C++ compiler. I was the first programmer to write OO code there.

I know also another telecom company, all their Java code are actually C, but with Java syntex.

Haha, what you pointed out is actually a 后门 for Java programmers to write C-like code... Of course, we can use main to 走后门 too...

We don't want to 开后门 or 走后门, do we??? Wink Smile Big Smile


gongshi edited on 2005-11-21 01:04

作者 Re:运行没有main函数的Java类 [Re:linux_china]
空心菜





发贴: 214
积分: 41
于 2005-11-24 15:11 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个是think in java 里面的例子
仔细看过这本书的,肯定有印象的
作者让读者演示给别人看的,没想到楼主把他放在这里了
理当放在水区

既然是放在java基础版,我觉得不如改下标题
如“静态变量的初始化过程”或“类最早初始化谁?”



作者 Re:运行没有main函数的Java类 [Re:linux_china]
wangyabing





发贴: 18
积分: 0
于 2005-12-29 20:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真好!主初学java的人感到它的无穷呀!


作者 Re:运行没有main函数的Java类 [Re:linux_china]
cjyzpcl





发贴: 64
积分: 4
于 2005-12-31 15:45 user profilesend a private message to usersend email to cjyzpclsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个我一般是放在类里面初始化读配置文件的。


欢迎大家就关于Java的问题赐教,多多学习,多多进步!
SCJP
SCWCD
SCBCD
SCEA
作者 Re:运行没有main函数的Java类 [Re:linux_china]
XiaoN





发贴: 3
积分: 0
于 2005-12-31 22:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这是《core java》里的一个代码


作者 Re:运行没有main函数的Java类 [Re:linux_china]
solo





发贴: 4
积分: 0
于 2006-01-01 18:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我想的是没有main函数的话,你要把什么编译成。EXE文件呢?



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923