开发者

Applet with dependencies in Eclipse

I'm fairly new to eclipse and java in general, so I'm sure this is pretty basic, but I can't find anything that points me in the right direction.

How do I run/test an applet in Eclipse that requires some 3rd party jars?

I have an application running with my dep开发者_如何学编程endent jars (JOGL to be specific) fine. And I can run a sample applet. But I can't run an applet that requires the JOGL jars. I understand the HTML for embedding it in a web page needs to reference the dependencies, but how do I tell eclipse where to look since it's not part of the standard java kit?

My ultimate need is to embed an application/applet IN a web page (not launched from, but running as in inline object on the page. I haven't gotten as far as trying to embed anything yet, but perhaps there's a corner I can cut there and I won't have to worry about the applet structure at all...


You do the same way as you do for a program having a main() method. Goto properties --> Build path --> Jars. Add your third party jars there and the program should pick them up.

First create a dummy third party jar which has a single method called hello(). You can convert that project into a jar by right clicking on the project and exporting it to a jar. Its very simple. The code for that third party jar is very simple.

package test;

public class ThirdParty {
    public String hello(){
        return "Hello from third party called!!";
    }
}

After this, create a simple applet called HelloApplet. All it has is just displaying an applet and in turn calling our third party jar. Adding the jar to the eclipse is shown as in the below screenshot.

Applet with dependencies in Eclipse

The source code for this TestApplet is very very simple as shown below.

import javax.swing.JApplet;

import test.ThirdParty;

public class TestApplet extends JApplet{
    public void init(){
        this.setSize(400, 400);
        this.setVisible(true);

        ThirdParty tParty = new ThirdParty();
        System.out.println(tParty.hello());
    }

}

Right click on the file and select Run as Applet. There you go! You would both see your applet and the message getting printed on the console as well! Any doubts you have, ask in comments section, I will try to help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜