Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

java applet classpath

Thread view: 
trpost@gmail.com - 19 Apr 2006 21:56 GMT
I have a simple applet that uses the (Jakarta http client .jar files)
to get a status of a webpage. When I execute the code from a command
line after setting the classpath, it runs as expected. However when I
call the applet through a browser, it fails to load and I get an
exception java.lang.NullPointer exception. I am running this applet
using IE 6 SP2 on Windows XP running apache. I set the classpath that
worked for the commandline execution in the system environment
variables, but it doesn't work any different. Below is the exception
from the java console that I am getting:

java.lang.NoClassDefFoundError:
org/apache/commons/httpclient/HttpException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.applet.AppletPanel.createApplet(Unknown Source)
    at sun.plugin.AppletViewer.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-11" java.lang.NullPointerException
    at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
    at sun.plugin.AppletViewer.showAppletException(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Any ideas how to get my applet to work in a browser??

Below are my 2 files:

StatusCodeApplet.java:

import java.lang.*;
import java.io.*;
import java.awt.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import javax.swing.JApplet;

public class StatusCodeApplet extends JApplet
{
 //Main method begins execution of java application
 //public static void main(String args[])
 //{
    //String url = "http://eagledssdsdasdsaf";
    //String result;
    //System.out.println(url);

    //result = getStatusCode(url);
    //System.out.println(result);
 //}

 public static String getStatusCode(String urlWithParams) {
   HttpClient client = new HttpClient();
   HttpMethod method = new GetMethod(urlWithParams);
   String returnStatusCode = "NO_STATUS_CODE";
   try {
     // Execute the method.
     int statusCode = client.executeMethod(method);
     // method.getStatusLine();
     returnStatusCode = Integer.valueOf(statusCode).toString();
   } catch (HttpException e) {
     System.err.println("Fatal protocol violation: " +
e.getMessage());
     e.printStackTrace();
   } catch (IOException e) {
     System.err.println("Fatal transport error: " + e.getMessage());
     e.printStackTrace();
   } finally {
     // Release the connection.
     method.releaseConnection();
   }
   return returnStatusCode;
 }

}

StatusCodeApplet.html:

<html>
<head>
<title>Http status code demo</title>
</head>
<body>
<script type="text/javascript">
function getStatusCode(uri)
{
 return document.applets['statusApplet'].getStatusCode(uri);

}

</script>

<applet name="statusApplet" code="StatusCodeApplet.class" width="500"
height="500" archive="commons-httpclient-3.0.jar"></applet>

<script type="text/javascript">
    //alert(getStatusCode('http://www.yahoo.com'));
</script>

</body>
</html>

The classpath I am using that works commandline is:
set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_06
set PATH=%JAVA_HOME%\bin;%PATH%
set
CLASSPATH=.;C:\cowpack\htdocs\php\client\applet\Tst\commons-httpclient-3.0.jar;C:\cowpack\htdocs\php\client\applet\Tst\commons-logging.jar;C:\cowpack\htdocs\php\client\applet\Tst\commons-logging-api.jar;C:\cowpack\htdocs\php\client\applet\Tst\commons-codec-1.3.jar

The way I complied the java file was
javac StatusCodeApplet.java

The way I access the applet is:
http://localhost/php/client/applet/Tst/StatusCodeApplet.html

Any suggestions, or what am I doing wrong

Thanks
andrewthommo@gmail.com - 20 Apr 2006 05:53 GMT
> I have a simple applet that uses the (Jakarta http client .jar files)
> to get a status of a webpage.

I suspect your applet code may need to be jar'd and signed to access
URL's at different sites, but that is obviously not the source of the
current problems.

> java.lang.NoClassDefFoundError:
> org/apache/commons/httpclient/HttpException

OK, it is not finding the Jakarta classes, ..

> Any ideas how to get my applet to work in a browser??
>
> Below are my 2 files:
>
> StatusCodeApplet.java:

This code is in the 'default' package, which is not recommended,
but also not the cause of the current problem.

> import java.lang.*;
> import java.io.*;
....
> public class StatusCodeApplet extends JApplet

Why a JApplet?  (as opposed to a java.applet.Applet)
...
> <html>
....
> <script type="text/javascript">
> function getStatusCode(uri)
> {
>   return document.applets['statusApplet'].getStatusCode(uri);
>
> }

Applets take some time to load and start.  You will probably
have better luck if you put a timeout/delay before checking
for the applet (whether your JS code is before or after the applet)
...
> <applet name="statusApplet" code="StatusCodeApplet.class" width="500"
> height="500" archive="commons-httpclient-3.0.jar"></applet>

This applet element, combined with this URL..

> The way I access the applet is:
> http://localhost/php/client/applet/Tst/StatusCodeApplet.html

Suggests to me that 'StatusCodeApplet.class' exists at..

http://localhost/php/client/applet/Tst/StatusCodeApplet.class

If that that is the case, the JVM is looking for the
'commons-httpclient-3.0.jar' at..

http://localhost/php/client/applet/Tst/commons-httpclient-3.0.jar

..is the jar in that location?

Andrew T.
trpost@gmail.com - 20 Apr 2006 21:31 GMT
yes all the files are in the same directory accessable through:
http://localhost/php/client/applet/Tst/...
andrewthommo@gmail.com - 26 Apr 2006 00:50 GMT
>> If that that is the case, the JVM is looking for the
>> 'commons-httpclient-3.0.jar' at..

>> http://localhost/php/client/applet/Tst/commons-httpclient-3.0.jar

>> ..is the jar in that location

> yes all the files are in the same directory accessable through:
> http://localhost/php/client/applet/Tst/...

OK.. (it helps to quote a little of earlier thread, like I did)

I'm stumped for the moment.  Are you *sure* that
'commons-httpclient-3.0.jar' contains the
org.apache.commons.httpclient.HttpException class?

Check it using the jar command or a Zip tool.

Can you make all your files accessible form the internet,
so that we can look at them?  It is a lot easier to debug
an applet directly (I could have checked both the file
locations and the Jar file by now).

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.