在小程序中拥有一个 main 方法是否合适?
我正在尝试了解小程序的使用规则。我意识到小程序不必有一个 main 方法(它可以使用 init()
代替),但我的教授提供的每个代码示例都是一个具有 main 方法的小程序,如下面的代码所示。
我的理解是,由于安全问题,在小程序中使用 main 方法不是一个好主意,这是正确的吗?什么时候(如果有的话)我应该在小程序中使用 main 方法?
另外,我的教授正在使用嵌入到 PowerPoint 演示文稿中的此类小程序,然后他可以在演示过程中轻松运行这些小程序。如果小程序使用 inti()
而不是 main()
,是否仍然可以运行嵌入到 PowerPoint 演示文稿中的小程序?我这么问是因为我看不出他有什么理由在这样的小程序中使用 main 方法,除非小程序在嵌入到 PowerPoint 演示文稿中时需要正确运行。
//EventDispatcherThreadDemo.java
import javax.swing.*;
public class EventDispatcherThreadDemo extends Japplet{
public EventDispatcherThreadDemo(){
add(new JLabel("Hi, it runs from an event dispatch thread"));
}
public static void main(Stirng[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame frame = new JFrame("EventDispatcherThreadDemo");
frame.add(new EventDispatcherThreadDemo());
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
PS 这与作业无关,只是想了解更多有关小程序的知识。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
main
方法对于小程序来说没有任何特殊含义。您的教授将其与几行代码一起包含在 JFrame 中以启动该类,以便也能够使用简单的“java foobar”命令从命令行运行它。这与必须运行“appletviewer foobar”命令相反,后者在从 PowerPoint 等其他程序启动时可能会出现问题。虽然它没有给出完整的Applet环境,但是演示程序可能很简单,这并不重要。
我不知道他为什么选择这样做,而不是仅仅向您展示一个包含小程序的大 HTML 页面。
The
main
method does not have any special meaning to an applet.Your professor is including it along with a few lines to fire up the class in a JFrame, to be able to run it from the command line too with a simple "java foobar" command. This as opposed to having to run the "appletviewer foobar" command which may have issues with being launched from another program like PowerPoint. it does not give the full Applet environment though, but the demonstration programs may be so simple, it doesn't matter.
Why he chose to do so instead of just presenting you with a big HTML page with the applets in, I do not know.
main() 对于小程序没有安全隐患,因为它不是由小程序容器调用的。即使是这样,小程序仍然会被限制在安全沙箱中。
OTOH,开发混合小程序/应用程序非常有意义。传统上基于框架的应用程序。比小程序更容易开发和调试。
如果您想向最终用户提供这两种形式,这也是有意义的。 ;)
请参阅这个混合示例,它可能有助于演示。
The main() has no security implications for an applet since it is not called by an applet container. Even if it was, the applet would still be confined to a security sandbox.
OTOH, developing a hybrid applet/application can make a lot of sense. Traditionally Frame based apps. were easier to develop and debug than applets.
It can also make sense if you want to offer both forms to the end user. ;)
See this example of a hybrid that might help demonstrate.
不幸的是,许多示例代码都采取了不应遵循的捷径。这里的危险在于,学习者不知道该模仿什么、不该模仿什么。
Applet 生命周期方法和
main
方法是向上调用的示例。与侦听器一样,它们应该很短,提取必要的信息,封装对上行调用的处理,并调用对该对象有意义的对象方法(特别是不会受到对上行调用的依赖的阻碍)。Unfortunately lots of example code takes short cuts which should not be followed. The danger here is that the learner is not in a good position what to imitate and what not to.
Applet life-cycle method and the
main
method are example of upcalls. Like listeners they should be short, extract necessary information, encapsulate handling of the upcall and call a method on an object that is meaningful to that object (in particular not encumbered by dependency on the upcall).实际上至少存在一个安全问题。假设您的小程序:
main
方法中留下了调试代码,该方法执行了一些潜在危险的操作,例如写入文件那么攻击者可以创建一个 Java Web Start 应用程序描述符,它指向您的 .jar 文件并以完全权限启动您的
main
方法。通过将名为
JNLP-INF/APPLICATION.JNLP
的空文件添加到您的 .jar 文件中,可以防御这种特定的攻击。但可能还有其他可能的攻击,所以我认为你的教授避免它是正确的。Actually there is at least one security issue. Suppose your applet:
main
method that does something potentially dangerous like write to a fileThen an attacker can create a Java Web Start application descriptor that points to your .jar file and launches your
main
method with full privileges.This specific attack can be defended against by adding an empty file called
JNLP‑INF/APPLICATION.JNLP
to your .jar file. But there may be other possible attacks, so I think your professor is right to avoid it.