java applet classpath

T

trpost

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
 
A

andrewthommo

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.
 
A

andrewthommo

If that that is the case, the JVM is looking for the
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.
 
Joined
May 12, 2006
Messages
2
Reaction score
0
.jar classpath - not sure

I don't think this is the solution. I have the same problem but with JDOM.

How could the JVM know what is the JAR name? That it is commons-httpclient-3.0.jar and not ugly-library.jar?

Maybe some dynamic class loading can be the solution? I will try...

Regards,
G.K.
 
Joined
May 12, 2006
Messages
2
Reaction score
0
Making applet include another jar

I've got it, at least for me it works well.

You have to add a classpath entry to the MANIFEST.MF file, for example by writing a manifest file yourself like that:

Manifest-Version: 1.0
Class-Path: commons-httpclient-3.0.jar

and by adding a "manifest" attribute to the ant "jar" target. Yes, you have to package your applet into a JAR (and specify it with "archive" attribute of the "applet" tag) but that is a good practice.

The JAR referenced by Class-Path should be in the same directory; if it is not, you can specify a relative path.

My applet is signed. I don't think it makes a difference as the commons-httpclient-3.0.jar is on the same machine but you might want to review the page:

http://mindprod.com/jgloss/applet.html

to find out more about applets. A great page.

Best regards,
Grzegorz Kaczor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top