3

I've got a question about a particularly annoying error that I haven't been able to figure out, much less overcome. Any time I try to run a Java applet (Applet or JApplet) on my website, I get this error as a pop-up:

 java.lang.reflect.InvocationTargetException

No stack trace, no line number, just the error message. So I've Googled around looking for anyone else's workarounds (or ideally actual fixes) but haven't been able to find much. I've tried several variations of my code (sometimes with a JAR file, sometimes not, sometimes a single class, sometimes not, sometimes in a package using a matching directory structure, sometimes no package, etc.) but can't seem to get past this nasty little son-of-a-bug. :)

For a specific example, here's my most recent attempt; first the Java code:

package cmtoolbox;

public class CMToolbox {
    public static void main(String[] args) {
        MainApplet a = new MainApplet();
    }
}

The class it sets up:

package cmtoolbox;

import javax.swing.JApplet;
import javax.swing.JButton;

public class MainApplet extends JApplet {
    public MainApplet() {
        JApplet main = new JApplet();
        main.setSize(800,600);
        JButton test1 = new JButton();
        test1.setText("Test");
        main.add(test1);
    }
}

My HTML code:

<html>
<head>
  <title> Experimenting with Java applets </title>
</head>
<body>
  <p><applet code="CMToolbox.class" width="800" width="600">
    I wish. :)
  </applet></p>
</body>
</html>

I suppose that maybe because the web itself can have so many variables (operating systems, browser types, etc.) there is something internal/system-level causing this... but I do have the JRE and JDK installed on my computer so I don't really get why... Anyway, I'm sure I'm not the first guy to hit this roadblock, but it's got me stumped so I'd appreciate any info that may be available on the subject. Also if you know of any good Java web tutorials for absolute noobs that would be great as well. :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2403876
  • 239
  • 2
  • 4
  • 16
  • Edit: I noticed just now there is no "setVisible" method, but I don't think that's it either because even with setVisible it still gives me the same thing. – user2403876 Aug 17 '13 at 05:02

2 Answers2

4

InvocationTargetException1 is thrown because the HTML is calling (trying to load) something that is not an applet. Change it to:

  <p><applet code="MainApplet" width="800" width="600">
  </applet></p>

Also, as mentioned in the answer of Stephen C. Move the stuff from the constructor into the init() method.

  1. InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.


While I'm here: Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +2 For mentioning that you shouldn't be using applets anyway. – Stephen C Aug 17 '13 at 07:12
  • Okay, I see your point - I have often thought applets were borderline obsolete anyway (lol). But what would be the alternative, then? Most of the Java web tutorials and resources I've seen deal with applets; so if coding applets is such a dumb idea (and again I get you on that), what else is there? Thanks. :) – user2403876 Aug 17 '13 at 15:44
  • Well the project I have in mind was going to be web-based because it would use a shared database among users (users would log in, add info etc). But that's not the only project I have in mind; I was also thinking a game or two, etc. Either way the idea was to get into putting my Java programs on the web. – user2403876 Aug 17 '13 at 16:44
  • *"shared database among users (users would log in, add info etc)."* A desktop app. can easily connect to a web-app. based front-end to a DB. If you want to 'launch it from a link' you could look to [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). 'Phoning home' to the same server (for the DB front-end) could be sand-boxed. – Andrew Thompson Aug 17 '13 at 16:51
  • *"also thinking a game or two, etc."* Again better as a free floating app. on the desktop, launched using JWS. – Andrew Thompson Aug 17 '13 at 16:52
  • Thanks for the info. I hadn't realized Java programs can connect to the internet without actually being "embedded" in a website. It does make more sense the way you describe tho. Thanks! :) – user2403876 Aug 20 '13 at 01:37
1

I suggest that you read the Oracle Applet Development Tutorial. I'm not an expert on applets (understatement!) but you seem to be doing a lot of things differently to how the Tutorial says to do them. For instance, you don't use a main method to launch an applet, and you should be doing the setup in the init method not the constructor (see here).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • @AndrewThompson - The documentation I linked to seems to say that `init` is preferable because the environment may not be ready when the constructor is invoked. – Stephen C Aug 17 '13 at 07:10