2687

How to run a Java Applet on Mac OS 10.6.6

Question:

I am in my first programming class and I am now reaching the end of the semester. I have an online portfolio where I share my college accomplishments. At this point in the semester, I would like to upload some of the simple Applets I have created to my online portfolio. My portfolio is hosted on Weebly.com. I tried uploading the files to the host site and using the simple tags to run the applet within the html. I'm quite sure I'm accessing the files using the correct directories. But before we jump to any conclusions there, I decided I ought to run the applets locally to ensure I'm doing everything correctly. I am on a macbook pro running OS 10.6.6. In Java Preferences, my Java SE versions are Java SE 6 64-bit and Java SE 6 32-bit. My plug-in version is 1.6.0 (found in /System/Library/Java/JavaVirtualMachines). These are the only versions I have on my machine. My research tells me that I may be having version disagreements. Some forums have suggested going back to plug-in version 1.5 (although, I have no idea how). I'm pretty sure now though that apple has updated Safari to a 64-bit version. I have Eclipse set to 1.6 also. Everything seems to be on the same page to me.

And yes, I've read all the related questions on here over and over. Most of them are a little outdated now.

Here's my applet code:

/** * Class NightScene - Draws a night scene (just for fun). * * @author Alex Stout * @version February 8, 2011 */ package lab05_1; import java.awt.*; import javax.swing.*; public class NightScene extends JApplet { /** * Paint method for applet. * * @param g the Graphics object for this applet */ public void paint(Graphics g) { g.setColor(Color.BLUE.darker().darker().darker()); g.fillRect(0,0, this.getWidth(), this.getHeight()); this.drawMoon(g); this.drawStars(g); this.drawHorizon(g); } public void drawStars(Graphics h) { for (int i = 0 ; i <= this.getWidth()*5; i++) { int x = (int)(Math.random()*this.getWidth()); int y = (int)(Math.random()*this.getHeight()); h.setColor(Color.WHITE); h.fillOval (x, y, (int) (Math.random()*3)+1, (int) (Math.random()*3)+1); } } public void drawMoon(Graphics j) { int x = (int)(Math.random()*(this.getWidth()-200)+50); int y = (int)(Math.random()*(this.getHeight()-200)+50); j.setColor(Color.YELLOW.brighter().brighter()); j.fillOval (x, y, this.getWidth()/10, this.getWidth()/10); j.setColor (Color.BLUE.darker().darker().darker()); j.fillOval (x-(this.getWidth()/100), y-(this.getWidth()/100), this.getWidth()/10, this.getWidth()/10); } public void drawHorizon(Graphics k) { int xi = 0; int xj = this.getWidth(); int yj = this.getHeight(); int rh = this.getHeight()-this.getHeight()/8; for (int i=0; i < xj; i++) { k.setColor(Color.BLACK); k.drawLine(xi, yj, xi, rh); k.setColor(Color.BLUE); if(Math.random()<0.50) { k.drawLine(xi++, rh++, xi, rh++); } else { k.drawLine(xi++, rh--, xi, rh--); } } } }

Here's my html code:

<html> <Applet code = NightScene.class codebase = "." width = "400" height = "400"> </Applet> </html>

Here's the Java Console output:

Java Plug-in 1.6.0_24 Using JRE version 1.6.0_24-b07-334-10M3326 Java HotSpot(TM) 64-Bit Server VM User home directory = /Users/myUserName

Some people have suggested using codebase = "." so I tried that to no avail. It doesn't work with or without it. I tried putting in the full directory path, no success. I tried quotes and no quotes around the class name. I tried with and without .class on the end. I tried making a lab05_1 subdirectory because that's the package name in the code. No luck. Both the class file and the html file are in the same folder on the desktop. The class file is a copy of the original one that was created in Eclipse, but it has the same name, so I wouldn't think this should cause any problems being in different directories.

I don't know what else to do. Please Help! This has been irking me for a week. I've spent hours upon hours on something so simple.

Answer1:

I don't have a Mac handy to check this, but if you change your HTML file, it should work --- you are missing the package name for the NightScene.class.

<html> <Applet code="lab05_1.NightScene.class" width="400" height="400"/> </html>

The Oracle reference for the APPLET tag is <a href="http://download.oracle.com/javase/6/docs/technotes/guides/deployment/deployment-guide/applet.html" rel="nofollow">here</a>.

From that page:

<blockquote>

<strong>CODE</strong> = <strong><em>appletFile</em></strong>

This REQUIRED attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. One of CODE or OBJECT must be present. The value <em>appletFile</em> can be of the form <em>classname.class</em> or of the form <strong><em>packagename.classname.class</em></strong>.

</blockquote> <hr />

<em>EDIT:</em> Just in case it's not clear, the directory should be laid out like so:

+-top-level/ | +-lab05_1/ | | | +-NightScene.class | +-test.html

Answer2:

Maybe old now, I just found this page in trying to answer a different question, but for what it's worth, looking at your original HTML and the ORacle link, I see you did not have quotation marks around the class file name. Suspect that may really have been the root of problem. In effect this is perhaps what 'Spong' is getting at too. I just didn't see any mention of the quotation marks which is what jumped out at me...

Recommend

  • Bluetooth debugging on android wear failing
  • mysql: select query when the column that has condition on has NULL value
  • remotely and programatically logoff an active domain user
  • Git command “add -p” on all files
  • How to iterate registry from the root?
  • Assign a firebase database url(String) to a Database Reference variable?
  • Using VSTS Rest API, how do you Update a Markdown widget?
  • Should I use redundancy or a simple query on a large dataset with Firebase Cloud Firestore database?
  • django filer - ImportError: cannot import name mixins
  • How to create mentions for names like “@myname” using javascript?
  • Stripe - PHP error - Stripe no longer supports API requests made with TLS 1.0
  • Jquery hide first 12 elementes, show next 12 elements
  • Why doesn't MSHTML for .Net have querySelector and querySelectorAll, or where are they?
  • Rapidly iterating Silverlight development
  • How can I do some cleanup right before an Android app is quitting?
  • Visual Studio extension: Change the hint path of an assembly reference
  • Mocking Non-Standard Events in F# Foq
  • Submission of new app with iAds
  • data.table replicate rows after join?
  • Can you build a truly RESTful service that takes many parameters?
  • xcode don't localize specific strings
  • Why cepheus don't send int without quotes to orion?
  • Sequential (transactional) API calls in angular 4 with state management
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Javascript Callbacks with Object constructor
  • Matrix multiplication with MKL
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • Acquiring multiple attributes from .xml file in c#
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Cant find why the layout is getting smaller
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal