Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dancks

macrumors regular
Original poster
Nov 8, 2009
100
0
I tried everything. I ask this question on Yahoo! Answers and I got basically what I saw everywhere else:
http://answers.yahoo.com/question/i...pnNOlGXsy6IX;_ylv=3?qid=20110807164512AAM4c68

in quotes:
I cannot for the life of me get images to load in an applet. I want to eventually make a game in java but I can't do that if I can't upload pics. I'm using a mac 10.5.8 and yes I only have Java 1.4.3 as Apple isn't releasing any new updates for older OSs apparently.

I asked this question before:
http://answers.yahoo.com/question/index;…

that's when I figured out it was because I didn't have 1.6.

I also tried running the applet provided here:
http://www.particle.kth.se/~lindsey/Java…

couldn't get that to work either. And I couldn't get a few others to work. Some of which were pretty old (they compiled after I switched out deprecated code.) wtf. Is it my java settings? what???



How was it done back in the day???

* 2 days ago

Additional Details
I also tried running ImageBlaster.java provided by Oracle on the MediaTracker page:
http://download.oracle.com/javase/1.4.2/…

I don't know if matters but I use BlueJ to compile and run these programs, usually in appletviewer.

answer:

Download the files from:

http://www.mediafire.com/?24p96ocv9cjef

There is an application to show a picture. Test this first.
Then there is an applet to show a picture. Test if this works by opening the .html file.
(Place all the files into the same folder)

It works with Java 1.4. so your version should not make a difference.

Internet Explorer has many security issues with applets, so I use FireFox to test my own applets saved on the hard disk.

I got his Year.java file to work but not the HelloApplet.java and HelloWord.html

It says The page “Hello-World Applet” attempted to load Java content, but Java is unavailable or not installed.

On my mom's iMac the page did appear to laod correctly but all that appeared was a black box.

I'm guessing the problem is a uninstalled java something or other but I have no idea what that would be since I have a lot of other java programs that work on my computer just not this kind of thing.

I'm using 10.5 and the highest version java available for my mac is 1.4.3.
 
It says The page “Hello-World Applet” attempted to load Java content, but Java is unavailable or not installed.
What browser was running when the message appeared?

Was Java enabled in the browser? It's often a separately enabled and disabled feature.
The location of the control depends on which browser.

On my mom's iMac the page did appear to laod correctly but all that appeared was a black box.
Screen shot? Which iMac model? Which OS version?

I'm guessing the problem is a uninstalled java something or other but I have no idea what that would be since I have a lot of other java programs that work on my computer just not this kind of thing.
Give a specific example. Name one of the "other java programs" on your computer.

What model is your computer? Is it 32-bit or 64-bit?

I'm using 10.5 and the highest version java available for my mac is 1.4.3.
I have a Mac mini with 10.5.8 on it, and it has Java 1.5. I think it was around 10.5.2 that the first Java 5 release came out for it. I also think it required a manual download and install at the time. Software Update would NOT see it or its subsequent updates until AFTER the first manual install. I could be misremembering, however.


First, identify exactly which version of Leopard you have, on which machine. If it's not 10.5.8, update to that first. If you have a reason not to update to 10.5.8, please explain that reason.

Run the "Java Preferences.app" program, which resides in the Utilities folder. Post what it shows for the available applet and application Java versions, under the General tab.

Next, run Software Update and post the exact list of available updates it shows you. Leave nothing out.

Then run Terminal.app and paste this line into a window:
Code:
java -version;  java -d64 -version
Copy and paste the output and post it.


To find the manually downloadable Java updates, go here:
http://support.apple.com/downloads/

Enter the following keywords into the keywords box.
Code:
java update 10.5
Press return to search.

You should see the following Java updates for 10.5:
Java for Mac OS X 10.5 Update 4
Java for Mac OS X 10.5 Update 5
Java for Mac OS X 10.5 Update 10

Download and install Update 4. Then run "Java Preferences.". Post what it shows for the available applet and application Java versions, under the General tab.

After Update 4 is installed and confirmed in "Java Preferences", rerun Software Update, and see if it sees an available Java update. If so, have it download and install it. Repeat this (with any intervening restarts) until after Update 10 has been successfully installed.

If you don't see Java 5 listed in "Java Preferences" after installing Update 4, post again. If Software Update sees no updates, post again. If it doesn't update all the way to Update 10, post again.

URLs:
Java for 10.5, update 4: http://support.apple.com/kb/DL848
Java for 10.5, update 5: http://support.apple.com/kb/DL924
Java for 10.5, update 10: http://support.apple.com/kb/DL1359
 
ok here are the requested pics:

iMac 10.6.8:
Screen shot 2011-08-10 at 10.01.16 PM.png

macbook(mine): 10.5.8
Picture 1.png

downloaded updates 8, 9, 10. 9 didn't need to be installed because a newer version existed apparently.

the result:
Picture 2.png


and yes java is enabled for safari:
Picture 3.png

So we got the black box to show up, so one mystery solved, although I could've sworn that didn't happen right after I downloaded the last update. Maybe I needed to restart the computer. I'm gonna download the safari update and see if that changes anything and I'll post back.

As far as the other java stuff I meant java examples in this book I bought where it had java gifs show up in a JFrame.
 
no it didn't change anything. Its exactly like the picture right above this post
 
I downloaded the mediafire examples, and the applet example is not going to work. Period.
It may have worked on some platforms at some time in the past, but it's not going to work on Mac OS X now. Period.

The reason it won't work is because it naively assumes it has access to the file-system. This is not allowed except for signed applets, and it isn't signed. Signing requires a jar-file, and it isn't in a jar-file.

One approach to loading images in an applet uses a MediaTracker, but only if the image is stored as a separate file on the HTTP server or the file-system. There is no MediaTracker in the example.

An alternative to a MediaTracker is to package images in a jar with the compiled classes. Then the classes can load the image from the jar without a MediaTracker. As already noted, the example has no jar.

So basically, the example applet has no possible chance of working as given.


Here's an example Java applet that use MediaTracker:
http://www.realapplets.com/tutorial/ImageExample.html

I found it by googling: java applet image example

Here's another example, found by googling: java getimage
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html


Both MediaTracker and jar-bundled image resources should be explained in any competent book or tutorial. What book or tutorial are you learning from, that it hasn't explained these? Be specific. Give title and author, or the URL of an online tutorial.

Also, give the publication date of the book, or the last date an online tutorial was updated. Things have changed a lot since the early days of Java, so it could be you're just using an outdated learning resource.


As far as the other java stuff I meant java examples in this book I bought where it had java gifs show up in a JFrame.
What is the title and author of the book? Is this the book you're using to learn how to write applets that load images?
 
The book was Java: Software Solutions by Lewis and Loftus. Its actually a really basic book and it didn't go this far in depth it just showed various java.awt functions but not applet. So I've been searching the internet for code examples to learn from. I think I prefer to learn that way instead of reading a book but if you got a good title I'll check it out. I'll check the O'Reilly series for something I guess.
 
It should be easy to find examples of applets. Or tutorials on applets. Example search terms:
java tutorial
java applet tutorial

If you have problems finding examples, post the search terms you're using.


You can download all the examples from "Java Examples in a Nutshell". There are 5 or 6 different editions of the book, as it evolved over time and different Java versions. Find the edition you want, and download the code.
http://oreilly.com/oreilly/cs/examples-faq.html

You can probably find a used paper-and-ink copy of "Java in a Nutshell", which is the companion explanation for "Java Examples in a Nutshell". It's also a fairly common book to find at your local public library. Or at a university, high school, or community college library.


A simple search tells me there are at least 6 editions of "Java: Software Solutions by Lewis and Loftus". Be more specific about exactly which book you have. Accuracy and specificity is important in programming.
 
I tried the Media Tracker example you provided. It doesn't work. Here's the java console:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:173)
at java.lang.ClassLoader.loadClass(ClassLoader.java:317)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:145)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:659)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:786)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:2389)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:715)
at sun.applet.AppletPanel.run(AppletPanel.java:369)
at java.lang.Thread.run(Thread.java:655)

I looked at the code it looks almost exactly like the other stuff I tried. I'm telling you Its gotta be the java version or the way java is set up on my machine. I just need a hint at what to check out to get things going. Is there a good "java on mac" tutorial or book or something out there?

I don't want to be this to be a "solve all of my problems" thread but I'm really over my head here.
 
This part of the error:
Code:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
tells me it's a class version-number problem. This usually indicates a Java version is older than the class it's trying to run. There can be other causes, but that's the most likely.

So the next step is to determine exactly what version of Java is running in your browser. If you saw a "Java Console" window in your browser, when the error trace was produced, it should identify the Java version your browser is using. Look there and post it.

If you don't see a Java Console window, enable it with:
Java Preferences.app
Advanced tab
Java Console item in list
Show console radio button
Also, please identify exactly which browser you're using, and which version. Be specific.

Then, identify if the browser is running in 32-bit or 64-bit. You can do that using "Activity Monitor.app". The row will show your browser name, and the column labeled "Kind" will show 64 bit or not.

Finally, use Java Preferences to rearrange the order of Java versions so 1.5 64-bit is above 1.6 64-bit. Or disable 1.6 64-bit and 1.4 32-bit.

EDIT
I uploaded a simple Java applet that identifies the Java version and OS version currently in use. It's compiled for Java 1.2 or later, so it should work on any Java version that was ever released on any Mac OS X version.

I may not leave it up there for long, so try it while you can.
 
Last edited:
This part of the error:
Code:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
tells me it's a class version-number problem. This usually indicates a Java version is older than the class it's trying to run. There can be other causes, but that's the most likely.

So the next step is to determine exactly what version of Java is running in your browser. If you saw a "Java Console" window in your browser, when the error trace was produced, it should identify the Java version your browser is using. Look there and post it.

If you don't see a Java Console window, enable it with:
Java Preferences.app
Advanced tab
Java Console item in list
Show console radio button
Also, please identify exactly which browser you're using, and which version. Be specific.

Then, identify if the browser is running in 32-bit or 64-bit. You can do that using "Activity Monitor.app". The row will show your browser name, and the column labeled "Kind" will show 64 bit or not.

Finally, use Java Preferences to rearrange the order of Java versions so 1.5 64-bit is above 1.6 64-bit. Or disable 1.6 64-bit and 1.4 32-bit.

EDIT
I uploaded a simple Java applet that identifies the Java version and OS version currently in use. It's compiled for Java 1.2 or later, so it should work on any Java version that was ever released on any Mac OS X version.

I may not leave it up there for long, so try it while you can.

Im using safari

and:

Picture 2.png

Picture 1.png

Picture 3.png

so I'm guessing from what I see there its 32 bit, since only java preferences is listed as 64 bit and in parenthesis.

and:

Picture 4.png

So I think thats what you wanted me to do but its still giving me the same result.
 
I tried it on the iMac. Couldn't get it to work either. I wonder if it might have something to do with the applet code and the way java is being used or something? I used a straightforward applet code <applet code:"myapplet.class ... that kind of thing. And I do get a little java cup to show up. On the iMac when it says error nothing appears in the java console, unlike my macbook shown above.

Also, I'm keeping this thread alive until I solve the problem. Didn't mean to double-post though.
 
Hi. If you are running an applet (not a stand alone app) then you need to run it using a web server... according to your safari path, it appears that you are trying to access it directly from the hard drive. (ie. file://)

After setting up a web server you would access it like:
http://localhost/myapp.html
 
Aww, my bad... when I open the html via the browser it does run correctly after all...

I used this code in my html:
<code>
<APPLET archive="TheApp.jar" code="theApp.MainClass.class" width=440 height=350>
</code>

sorry to lead you down a wrong path, haven't used java in a while.
 
Last edited:
Modified the java code because I was also getting a black screen like you had from the code you specified in the dl link:

Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.*;

//I think you need to go to the Advanced tab of Internet Options
// and enable "Allow Active Content to run in files on my computer"
 

public class HelloApplet extends Applet
{
    public void paint (Graphics g)
    {
		try
		{
			setBackground (Color.black);

			MediaTracker mediaTracker = new MediaTracker(this);

			String s = String.format("year.jpg");
			Image image = getImage(getCodeBase(), s);
			mediaTracker.addImage(image, 0);
			mediaTracker.waitForAll();

			g.drawImage(image, 60, 70, null);
		}
		catch(Exception ex)
		{
		}
		/*
		Image pic = Toolkit.getDefaultToolkit().getImage("year.jpg");
		if (pic != null)
		{
	    	g.drawImage (pic, 60, 70, this);
		}
		*/
    }
}

html
Code:
<html>
<head>
  <title>Hello-World Applet</title>
</head>
<body>
  <p>Applet to display a picture</p>
  <applet code="HelloApplet.class" width="400" height="300">
  </applet>
</body>
</html>
 

Attachments

  • year.jpg
    year.jpg
    24.3 KB · Views: 862
hey it worked. Thanks RDO!

well, it worked on the iMac 10.6, not my macbook 10.5
 
Last edited:
I don't know what else could be the problem then...
You followed chown33's advice on the 10.5 pc too, right?

You might try clearing the cache and then reloading the applet...
 
I did everything he told me to do. I'm starting school in less than a week I'll ask some of the smart people hanging around there what I'm doing wrong. I'm sure they'll have a good laugh at my expense first but its all worth it. I'll look around and try different stuff when I finally have it figured out I'll post the solution
 
I put the image on my desktop on the 10.5 and it worked like a charm. The dumbass award goes to yours truly.

I'm just curious why that would matter. Would it only be because that's where I saved the java code?
 
lol. it happens sometimes...

yes, wherever the compiled applet is located; getCodeBase() -
Gets the base URL. This is the URL of the directory which contains this applet.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.