• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

appletviewer

 
Dan Schwartz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic