Java NoClassDefFoundError
Here I am downloading a web-page source code th开发者_开发百科en storing it in text file. Then I read that file and match it with a regex to search for a specific string.
There is no compiler error.
Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/CharSequence
Can anybody tell me Where I am wrong.
java version "1.3.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WebDownload
{
public void getWebsite()
{
try{
URL url=new URL("www.gmail.com");// any URL can be given
URLConnection urlc=url.openConnection();
BufferedInputStream buffer=new BufferedInputStream(urlc.getInputStream());
StringBuffer builder=new StringBuffer();
int byteRead;
FileOutputStream fout;
StringBuffer contentBuf = new StringBuffer();
while((byteRead=buffer.read()) !=-1)
{
builder.append((char)byteRead);
fout = new FileOutputStream ("myfile3.txt");
new PrintStream(fout).println (builder.toString());
fout.close();
}
BufferedReader in = new BufferedReader(new FileReader("myfile3.txt"));
String buf = null;
while ((buf = in.readLine()) != null) {
contentBuf.append(buf);contentBuf.append("\n");
}
in.close();
Pattern p = Pattern.compile("<div class=\"summarycount\">([^<]*)</div>");
Matcher matcher = p.matcher(contentBuf);
if(matcher.find())
{
System.out.println(matcher.group(1));
}
else
System.out.println("could not find");
}
catch(MalformedURLException ex) {
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
}
}
public static void main(String [] args)
{
WebDownload web=new WebDownload();
web.getWebsite();
}
}
Note that you can usually refer to the javadoc to determine if an API is available in the version of Java you're using. For example, the javadoc for CharSequence states "Since: 1.4".
I generally keep a link to the latest released API javadocs easily accessible.
From the API :: java.lang.NoClassDefFoundError - Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
What it sounds like is you are either lacking needed libraries or they are not in your PATH. I'd check there first.
Additionally check you JAVA_HOME\lib for the classes you are importing.
Are you maybe using a 1.3 JVM to execute the code? This would explain the error, if the code was compiled with 1.3 compatibility (or lower), but against 1.4 or later JRE libaries.
Looks like your are running your app with java 1.3. Can you type java -version
and paste the output?
On what os are you? How are you running your app?
The CharSequence
Interface has only existed since Java 1.4, but you are running Java 1.3. This Interface is used by the StringBuffer
class that you are using in your code. The current version of Java is Java 6 - I would suggest updating if you can.
精彩评论