开发者

Java.lang.NoClassDefFoundError java.lang.StringBuilder from code using java.lang.StringBuffer

I know there are plenty of questions regarding this error, but I haven't found it easy to find one exactly like mine.

Here goes.

I have a pretty small application that writes DB data to a csv file and then uploads it to a server. I have it running on my local box out of eclipse which is great, but the final version needs to be run as a cron job on a server box from command line.

I'm working on the command line call for the main java class and its giving me lots of trouble.

My dependency structure is as such: My classes

  • package=gtechReconcile
  • classes= GtechReconciler.java, CSVFile.java, QueryMachine.java, FTPSender.java

My external libs

  • ojdbc14.zip
  • edtftpj.jar

I run the following line in the terminal in the directory above the compile source package (ie /path/to/classes where /path/to/classes/gtechReconcile/ has all the compiled class files) This path is the current directory when the command line is run :

java -cp /<snip>/lib/ojdbc14.zip:/<snip>/lib/edtftpj.jar:/path/to/classes gtechReconcile.GtechReconciler

The error then tells me it can't find java.lang.StringBuilder, even though this does not exist in my code. I use a StringBuffer, which apparently StringBuilder has replaced since Java5. Perhaps the java compiler converted this to StringBuilder, which the jvm then does not resolve?

What am I missing here?

EDIT : added error from prompt (clarified where this executes from and what is present in the package folder):

[gtechReconcile]$ pwd
/path/to/
[gtechReconcile]$ cd classes/gtechReconcile/
[gtechReconcile]$ ls
CSVFile.class  FTPSender.class  GtechReconciler.class  QueryMachine.class  reports
[gtechReconcile]$ cd ..
[classes]$ ls
gtechReconcile
[classes]$ java -cp .:/path/to/lib/ojdbc14.zip:/path/to/lib/edtftpj.jar gtechReconcile.GtechReconciler
Creating file
Exception in thread "main" java.lang.NoClassDefFoundError: while resolving class: gtechReconcile.CSVFile
at java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
at java.lang.Class.initializeClass() (/usr/lib/libgcj.so.5.0.0)
at gtechReconcile.GtechReconciler.main(java.lang.String[]) (Unknown Source)
Caused by: java.lang.ClassNotFoundException: java.lang.StringBuilder not found in [file:./, file:/opt/mms_tstt2/gtechReconcile/lib/ojdbc14.zip, file:/opt/mms_tstt2/gtechReconcile/lib/edtftpj.jar, file:/usr/share/java/libgcj-3.4.6.jar, file:./, core:/]
at java.net.URLClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.5.0.0)
at gnu.gcj.runtime.VMClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.5.0.0)
at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/libgcj.so.5.0.0)
at _Jv_FindClass(_Jv_Utf8Const, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0)
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0)
at _Jv_BytecodeVerifier.verify_instructi开发者_如何学Goons_0() (/usr/lib/libgcj.so.5.0.0)
at _Jv_VerifyMethod(_Jv_InterpMethod) (/usr/lib/libgcj.so.5.0.0)
at _Jv_PrepareClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
at _Jv_WaitForState(java.lang.Class, int) (/usr/lib/libgcj.so.5.0.0)
at java.lang.VMClassLoader.linkClass0(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
at java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
...2 more


The exception is raised when you create a new StringBuilder in your CSVFile class. Google tells me it's a frequent problem with GCJ, use an official Sun JVM. It usally resolves this kind of problem.


The gtechReconcile folder is assumed to contains all the class files in the gtechReconcile package.

In example, if you have /path/to/classes/gtechReconcile/GtechReconciler.class, you have two options.

  1. Go to /path/to/classes, add to the classpath the current directory (.) and run java as you did before.
  2. Add /path/to/classes to the classpath and run java wherever you like.


The default classpath (if you don't specify one) is the current directory (".").

However, if you specify a classpath, java will use that instead of the default, thus removing the current directory.

The solution to your problem is simple: add the "current directory" to your class path, such as:

java -cp .:/<snip>/lib/ojdbc14.zip:/<snip>/lib/edtftpj.jar:/<snip>/bin/ gtechReconcile.GtechReconciler
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜