This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Applets and the fly likes appletviewer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "appletviewer" Watch "appletviewer" New topic
Author

appletviewer

Dan Schwartz
Greenhorn

Joined: Oct 04, 2013
Posts: 5
I'm trying to get appletviewer to work with a very simple Hello program. Here's my code for test.java

import java.applet.*;
import java.awt.*;
public class test extends Applet {
public void paint(Graphics g) {
g.drawString("Hello",123,125);
}
}
Then I wrote the following html. Here's my code for test.html

<applet code = "test" width = 300 height = 300>
</applet>

When I execute the command: appletviewer test.html Everything works great.

I run into trouble when I try add a package label at the top of my java file. Add line #1 starts with:

package applet;

I find I can't get the appletviewer to work. I get an error that reads:

java.lang.NoClassdefFoundError: test (wrong name: applet/test)

From a book I'm reading (Core Jave, Volume I - Fundamentals) I see that I should change my html file as follows (adding applet/ before the name test):

<applet code = "applet/test" width = 300 height = 300>
</applet>

Now I get the error:

load: class applet/test not found.

I suspect my problem is in my html syntax but I can't figure it out. I've tried applet.test, applet//test and several others but nothing works. How can I get appletviewer to recognize my applet if it has a package name?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Welcome to the Ranch.

Looks like you forgot the .class extension.
Try



On a related note
1) You are extending Applet which is AWT. Instead you should be using the the swing substitute JApplet. Swing offers direct one to one replacements, typically prefixed with the 'J' e.g. Panel (AWT) -> JPanel (Swing), Button -> JButton etc
2) You have named your class 'test' Coding convention recommends it should be Test


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
I think the ".class" extension is usually unnecessary. But as Maneesh showed, dots are used to separate packages and classes, not slashes.

Make sure you have a directory called "applet" in the same directory as the HTML file, and that the test.class file is in there.

(If you do use Swing at some point, note that there are some other dissimilarities to AWT, like using paintComponent instead of paint.)


Ping & DNS - my free Android networking tools app
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Yup. Even I am unsure about the .class extension. (It's ages I worked with applets)
However both these specify it
http://docs.oracle.com/javase/1.4.2/docs/guide/misc/applet.html
http://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/runAppletFunction.html
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
The confusing thing about the code parameter is that if ".class" is required, then it seems the parameter would be a file name - which naturally would use slashes between package and class name. But it wants dots, indicating that it is a class name - which would not have the ".class" extension. I think that's why most (all?) JVMs do not require the ".class" at the end, even though the docs say it should be there.

But to get back to Dan's issue, I think the problem is more likely to be that the test.class file is not in a directory called applet.
Dan Schwartz
Greenhorn

Joined: Oct 04, 2013
Posts: 5
Thank you all for your posts. Unfortunately I still can't get this to work. I think maybe I didn't provide enough detail. I'm running windows 7 and doing all my work now from a cmd prompt. I created a directory off of the root and called it C:\temp5. That's where you'll find test.class, .html, and .class. My code is listed below. I run "appletviewer test.html" directly from c:\temp5. I also created a directory C:\temp5\applet and copied test.* over there as well. I tried everything from C:\applet too. I used all sorts of combinations running appletviewer from C:\, C:\applet, C:\temp5, C:\temp5\applet. For everything I try an applet window opens but the word Hello doesn't appear. As stated in my first post with the "package applet;" line removed from my code and the html looking to "test" only it works fine; I get an applet window with the word Hello in it.

I respectfully ask you to take another look (can't thank you enough!). Let me know if the java is correct, if the html is correct, what directory, or directories should these files reside in, and what directory should I run "appletviewer test.html" from.

package applet;
import java.applet.*;
import java.awt.*;

public class test extends Applet {
public void paint(Graphics g) {
g.drawString("Hello",123,125);
}
}


<applet code = "applet.test.class" width = 300 height = 300>
</applet>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
I created a directory off of the root and called it C:\temp5. That's where you'll find test.class, .html, and .class. My code is listed below.

Move test.class into a directory called C:\temp5\applet. What is the difference between "test.class" and ".class"?
Dan Schwartz
Greenhorn

Joined: Oct 04, 2013
Posts: 5
I meant to say that test.class, .html and .java all reside in C:\temp5. They also reside in C:\temp5\applet and C:\applet. I've tried to run the command "appletviewer test.html" from each of these directories. In every case if I comment out the java program's "package applet;" tag set my html code tag to: <applet code = "test" ... it works just fine. However if I put the package statement back in, everything I try with the html file "applet/test", "applet.test", "applet/test.class" and "applet/test" fails. I get a java.lang.ClassNotFound Exception: followed by some variation of 'applet.test".

With the package statement in place, I simply can't figure out how to configure the html file to see my test.class file. Seems like a simple problem, and I do really appreciate everyone's help. This is frustrating.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
You need to run it in C:\temp5. Both "applet/test" and "applet/test.class" are incorrect; see above. It's perhaps best to delete all other class files, so that you can be sure appletviewer doesn't get confused.
Dan Schwartz
Greenhorn

Joined: Oct 04, 2013
Posts: 5
OK, all files have been deleted except for the 3 files that reside in the C:\temp5 directory; test.java, .html and .class. Here's the code:

Java applet:
package applet;
import java.applet.*;
import java.awt.*;

public class test extends Applet {
public void paint(Graphics g) {
g.drawString("Hello",123,125);
}
}

HTML file:
<applet code = "applet.test.class" width = 300 height = 300>
</applet>

From the C:\temp5 directory I execute the command: appletviewer test.html

An applet loads but the Hello text doesn't display in it. At the bottom of the window it reads "Start applet not initialized". At the command prompt I see:

load: class applet.test.class not found.
java.lang.ClassNotFoundException: applet.test.class


Any thoughts? Can anyone get this to work?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
OK, all files have been deleted except for the 3 files that reside in the C:\temp5 directory; test.java, .html and .class.

Re-read what I wrote two posts back - the class file needs to be in C:\temp5\applet, so it corresponds with the package hierarchy.
Dan Schwartz
Greenhorn

Joined: Oct 04, 2013
Posts: 5
Thanks for the clarification. It works now. C:\temp5\test.html, followed by C:\temp5\applet\test.class. Then from c:\temp5 I run test.html

Thank you, thank you, thank you!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: appletviewer