Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java applet classpath

1 view
Skip to first unread message

trp...@gmail.com

unread,
Apr 19, 2006, 4:56:42 PM4/19/06
to
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

andrew...@gmail.com

unread,
Apr 20, 2006, 12:53:20 AM4/20/06
to
trp...@gmail.com wrote:
> 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..

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.

trp...@gmail.com

unread,
Apr 20, 2006, 4:31:28 PM4/20/06
to

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

andrew...@gmail.com

unread,
Apr 25, 2006, 7:50:29 PM4/25/06
to
>> 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.

0 new messages