NoClassDefFoundError eventhough class in in same folder - java

I executed a main class and got the following error and trace.
This is the console command:
java -cp . net.sf.tinyPayroll.Main
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.hsqldb.Trace
at org.hsqldb.Database.reopen(Unknown Source)
at org.hsqldb.Database.open(Unknown Source)
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
at org.hsqldb.DatabaseManager.newSession(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
at org.hsqldb.jdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at net.sf.tinyPayroll.dao.DBConnector.connectDataFile(DBConnector.java:88)
at net.sf.tinyPayroll.dao.DBConnector.<init>(DBConnector.java:72)
at net.sf.tinyPayroll.dao.DBConnector.getInstance(DBConnector.java:106)
at net.sf.tinyPayroll.model.DataFile.<init>(DataFile.java:53)
at net.sf.tinyPayroll.Main.main(Main.java:42)
However, all the necessary classes are in the same folder.
Here is the file which is extracted and available in the same folder (the entire library is available in extracted form).
find . -name Trace*
./org/hsqldb/Trace.class
./org/hsqldb/util/Traceable.class

Your exception is:
NoClassDefFoundError: Could not initialize class org.hsqldb.Trace
Which doesn't mean that it cannot find the class org.hsqldb.Trace in your classpath, it means that the class could not be initialized for some reason.
It generally means that a RuntimeException was thrown while either trying to assign a value to a static field or while trying to execute some code in a static block.
For example we will get such issue in the next cases:
class Trace {
static MyClass foo = MyClass.newInstance(); // If it fails while calling newInstance
static {
SomeClass.init(); // If it fails while calling SomeClass.init()
}
...
}

As Nicolas has mentioned this doesn't mean that it cannot find the class org.hsqldb.Trace in your classpath, it means that the class could not be initialized for some reason.
I've checked the code (This might change based on version) http://grepcode.com/file/repo1.maven.org/maven2/hsqldb/hsqldb/1.8.0.1/org/hsqldb/Trace.java
In the class it has some static block which do some processing. Most probably some of the resources are missing in your class path

Related

Setting up the JavaSci in CMD

Peace be upon you,
Based on this link, I followed the required steps for setting up the JavaSci in Windows; but I encounter this problem
c:\Backups>javac -cp "C:\Program Files\scilab-5.5.0\modules\javasci\jar\org.scilab.modules.javasci.jar;C:\Program Files\scilab-5.5.0\modules\types\jar\org.scilab.modules.types.jar";. TestSciLab.java
c:\Backups>set PATH=%PATH%;"C:\Program Files\scilab-5.5.0\bin"
c:\Backups>java -cp "C:\Program Files\scilab-5.5.0\modules\javasci\jar\org.scilab.modules.javasci.jar;C:\Program Files\scilab-5.5.0\modules\types\jar\org.scilab.modules.types.jar";. TestSciLab
The native library javasci does not exist or cannot be found.
java.lang.UnsatisfiedLinkError: no javasci in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at org.scilab.modules.javasci.Call_ScilabJNI.<clinit>(Unknown Source)
at org.scilab.modules.javasci.Call_Scilab.SetFromJavaToON(Unknown Source)
at org.scilab.modules.javasci.Scilab.initScilab(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at TestSciLab.main(TestSciLab.java:7)
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.scilab.modules.javasci.Call_ScilabJNI.SetFromJavaToON()V
at org.scilab.modules.javasci.Call_ScilabJNI.SetFromJavaToON(Native Method)
at org.scilab.modules.javasci.Call_Scilab.SetFromJavaToON(Unknown Source)
at org.scilab.modules.javasci.Scilab.initScilab(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at TestSciLab.main(TestSciLab.java:7)
c:\Backups>
The only difference that you see in the above screen and the guides in the link is the place of %path% that I moved it to the beginning. But do not doubt! even I checked it with the reverse order and I got the error.
My code is as simple as
import org.scilab.modules.javasci.JavasciException.InitializationException;
import org.scilab.modules.javasci.Scilab;
public class TestSciLab {
public static void main(String[] args) throws InitializationException {
Scilab sci = new Scilab();
}
}
Any lighting up points?
On Windows, you just need to add Scilab's bin folder to %PATH%.

Can't cope with packages

In C:\ru\compscicenter\java\stacks> I have placed
AbstractStack.java
ArrayStack.java
LinkedStack.java
Stack.java
StackTest.java
The first string of each of these files is
package ru.compscicenter.java.stacks;
My my CLASSPATH examination:
C:\ru\compscicenter\java\stacks>echo %CLASSPATH%
.;C:\ru\compscicenter\java\stacks
When I'm in the stacks directory and try to compile StackTest, I fail to do that.
What I write and what I get is here:
C:\ru\compscicenter\java\stacks>javac StackTest.java
StackTest.java:4: error: cannot find symbol
public static void fill(Stack<String> stack){
^
symbol: class Stack
location: class StackTest
StackTest.java:12: error: cannot find symbol
public static <E> void dump(Stack<E> stack){
^
symbol: class Stack
location: class StackTest
StackTest.java:24: error: cannot find symbol
LinkedStack<String> stack = new LinkedStack<String>();
^
symbol: class LinkedStack
location: class StackTest
StackTest.java:24: error: cannot find symbol
LinkedStack<String> stack = new LinkedStack<String>();
^
symbol: class LinkedStack
location: class StackTest
StackTest.java:29: error: cannot find symbol
ArrayStack<String> stack = new ArrayStack<String>(10);
^
symbol: class ArrayStack
location: class StackTest
StackTest.java:29: error: cannot find symbol
ArrayStack<String> stack = new ArrayStack<String>(10);
^
symbol: class ArrayStack
location: class StackTest
6 errors
Could you help me correct this?
Added later:
Then I did this:
C:\>javac c:\ru\compscicenter\java\stacks\*.java
Note: c:\ru\compscicenter\java\stacks\ArrayStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\>java StackTest
Exception in thread "main" java.lang.NoClassDefFoundError: StackTest (wrong name: ru/compscicenter/java/stacks/StackTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Could you help me again?
While compling use something like this
Javac -d . *.java
it will automatically create package structure and place class files in corresponding packages
The packages are interpreted as relative to the current directory. If you are specifying
package ru.compscicenter.java.stacks;
then the .java and .class files need to be in the directory ru\compscicenter\java\stacks\ from wherever you're running the commands. In this case, you need to run javac and java from C:\.
Start by defining a root directory for your app. Placing everything directly in c:\ is really not a good idea:
c:\myapp
then create a directory for the sources (.java files):
c:\myapp\src
and another one for your classes (.class files):
c:\myapp\classes
Now, your source tree should match exactly with your package tree, so you should have your sources files under
c:\myapp\src\ru\compscicenter\java\stacks
since they're all in the package ru.compscicenter.java.stacks.
Place yourself at the root of your app, in c:\myapp. The javac compiler expects files as arguments. And you want to place the compiled .class files in c:\myapp\classes. So you want to use
javac -d classes src\ru\compscicenter\java\stacks\*.java
This will create a folder tree matching exactly with the package tree in c:\myapp\classes, containing the compiled .class files. The root of the tree is c:\myapp\classes, so that's what you need to add to the classpath to run the app. And java expects a fully qualified class name:
java -cp c:\myapp\classes ru.compscicenter.java.stacks.StackTest
or, using a relative path, since you're in c:\myapp already:
java -cp classes ru.compscicenter.java.stacks.StackTest

java.lang.RuntimeException: java.lang.ClassNotFoundException: <org.objectweb.asm.ClassWriter.getCommonSuperClass(Unknown Source)>

I get following exception with new cobertura (2.0.2..). I guess it is some how related to new object creation immediately in a new block.
WARN instrumentClass, Unable to instrument file c:\apps\ijprojects\TrickyInstrument\out\production\TrickyInstrument\InstrumentationFailsOnFirstNewClassInTryBlock.class
java.lang.RuntimeException: java.lang.ClassNotFoundException: DataAccess
at org.objectweb.asm.ClassWriter.getCommonSuperClass(Unknown Source)
at org.objectweb.asm.ClassWriter.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitMaxs(Unknown Source)
at org.objectweb.asm.util.CheckMethodAdapter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitMaxs(Unknown Source)
at org.objectweb.asm.commons.LocalVariablesSorter.visitMaxs(Unknown Source)
at org.objectweb.asm.tree.MethodNode.accept(Unknown Source)
at org.objectweb.asm.util.CheckMethodAdapter$1.visitEnd(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitEnd(Unknown Source)
at org.objectweb.asm.util.CheckMethodAdapter.visitEnd(Unknown Source)
at org.objectweb.asm.ClassReader.b(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at net.sourceforge.cobertura.instrument.CoberturaInstrumenter.instrumentClass(CoberturaInstrumenter.java:204)
at net.sourceforge.cobertura.instrument.CoberturaInstrumenter.instrumentClass(CoberturaInstrumenter.java:121)
at net.sourceforge.cobertura.instrument.CoberturaInstrumenter.addInstrumentationToSingleClass(CoberturaInstrumenter.java:233)
at net.sourceforge.cobertura.instrument.Main.addInstrumentationToSingleClass(Main.java:274)
at net.sourceforge.cobertura.instrument.Main.addInstrumentation(Main.java:283)
at net.sourceforge.cobertura.instrument.Main.addInstrumentation(Main.java:292)
at net.sourceforge.cobertura.instrument.Main.parseArguments(Main.java:373)
at net.sourceforge.cobertura.instrument.Main.main(Main.java:395)
8 Jul, 2013 2:05:07 PM net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler saveCoverageData
INFO: Cobertura: Saved information on 2 classes.
The following is the code related to above exception.
public class InstrumentationFailsOnFirstNewClassInTryBlock {
public void saveToDatabase() {
//
try {
// boolean b=false;
// if ( b) {
// System.out.println("no action");
// }
DataAccess da = new DataAccess();
System.out.println("nothing");
} catch (Exception e) {
}
}
}
class DataAccess {
public DataAccess() {
//To change body of created methods use File | Settings | File Templates.
}
}
If I un-comment the code block some dummy statements , then instrumentation works fine. Has any one seen this? Any potential fixes?
Edit: Error occurs with java6 and java7.
Original problem was due to a Cobertura defect. It is not fixed. Cobertura now supports an additional argument for auxillary classpath.. This will be used to resolve any classes required for instrumentation.
cobertura-ant task documentation
Adding auxClasspath
auxClasspath argument is designed to remove the ClassNotFoundException
during instrumentation. See
https://github.com/cobertura/cobertura/wiki/FAQ#classnotfoundexception-during-instrumentation
for more information on this argument
I had a similar issue, and it may be a bug, see: https://github.com/cobertura/cobertura/issues/49
Your test case may be useful to debug the issue...
From https://github.com/cobertura/cobertura/wiki/FAQ#classnotfoundexception-during-instrumentation:
"This is because during instrumentation in cobertura 2.0, we use ASM to rebuild the .class files. We rebuild the stackmap which is a requirement to be compatible with java 7 and anything after. This does not mean that we recompile the code, however ASM requires that we provide the binaries of the other classes just in case it needs to look up any super methods. To fix this we use an argument called auxClasspath."
Adding the following code to your ant file (build.xml) should resolve the issue.
<path id="cobertura.auxpath">
<pathelement location="${bin}"/>
</path>
<target name="instrument_coverage" depends="init_coverage"
description="Instruments source code for coverage measurement">
<cobertura-instrument datafile="${coverage.datafile}">
<fileset refid="coverage-files"/>
<auxClasspath>
<path refid="cobertura.auxpath" />
</auxClasspath>
</cobertura-instrument>
</target>
This worked for me.

InvalidClassException : class invalid for deserialization

I am using SSA parser library in my project. When I invoke main method of one of it's class using command prompt it works fine on my machine.
I execute following command from command prompt :
java -Xmx800M -cp %1 edu.stanford.nlp.parser.lexparser.LexicalizedParser -retainTMPSubcategories -outputFormat "penn,typedDependenciesCollapsed" englishPCFG.ser.gz %2
But when I tried to use the same class in my java program, I am getting Caused by: java.io.InvalidClassException: edu.stanford.nlp.stats.Counter; edu.stanford.nlp.stats.Counter; class invalid for deserialization exception.
Following line throws error :
LexicalizedParser _parser = new LexicalizedParser("C:\englishPCFG.ser.gz");
This englishPCFG.ser.gz file contains some classes or information which gets loaded when creating object of type LexicalizedParser.
Following is the stacktrace :
Loading parser from serialized file C:\englishPCFG.ser.gz ...
Exception in thread "main" java.lang.RuntimeException: Invalid class in file: C:\englishPCFG.ser.gz
at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserDataFromSerializedFile(LexicalizedParser.java:822)
at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserDataFromFile(LexicalizedParser.java:603)
at edu.stanford.nlp.parser.lexparser.LexicalizedParser.<init>(LexicalizedParser.java:168)
at edu.stanford.nlp.parser.lexparser.LexicalizedParser.<init>(LexicalizedParser.java:154)
at com.tcs.srl.ssa.SSAInvoker.<init>(SSAInvoker.java:21)
at com.tcs.srl.ssa.SSAInvoker.main(SSAInvoker.java:53)
Caused by: java.io.InvalidClassException: edu.stanford.nlp.stats.Counter; edu.stanford.nlp.stats.Counter; class invalid for deserialization
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserDataFromSerializedFile(LexicalizedParser.java:814)
... 5 more
Caused by: java.io.InvalidClassException: edu.stanford.nlp.stats.Counter; class invalid for deserialization
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
... 17 more
I am new to Java world so I dont to why this error is coming and what should I do to avoid it.
I googled for this error then I found out that this error comes because of some version mismatch which I think is something similar to dll hell of windows API. Am I correct?
Anyone knows why this kind of error comes? and what should we do to avoid it?
Please enlighten !!!
It could be because the serialVersionUID of the classe has changed, and you are trying to read an object that was written with another version of the class.
You can force the version number by déclaring a serialVersionUID in your serializable class:
private static final long serialVersionUID = 1L;
The java word for dll hell is classpath hell ;-) But that's not your hell anyway.
Object serialization is a process of persisting java objects to files (or streams). The output format is binary. Deserialization (iaw: making java objects from serialized data) requires the same versions of the classes.
So it is possible, that you simply use an older or newer version of that Counter class. This input file should be shipped with a documentation that clearly says, which version of the parser is required. I'd investigate in that direction first.
OT: For the sake of completeness I ran into InvalidClassException ... class invalid for deserialization (and this question) when solving another problem.
(Since edu.stanford.nlp.stats.Counter is not anonymous, the case in this question is certainly not the same case as mine.)
I was sending a serialized class from server to client, the class had two anonymous classes. The jar with these classes was shared among server and client but for server it was compiled in Eclipse JDT, for client in javac. Compilers generated different ordering of names $1, $2 for anonymous classes, hence instance of $1 was sent by server but could not be received as $1 at client side. More info in blogpost (in Czech, though example is obvious).
Try using serialVer to generate the serialID of your old classes that you're trying to de-serialize and add it explicitly (private static final long serialVersionUID = (insert number from serialVer here)L;) in the new versions of the class. If you change anything in a class serialized and you haven't setted the serialID, java thinks the class you've serialized isn't compatible with the new one.
This error suggests that the serialized objects within C:\englishPCFG.ser.gz were serialized with using a older or newer definition of the class which unfortunately is different in such a way that it breaks the terms of compatible serialization from one version to another.
Please see http://download.oracle.com/javase/1.4.2/docs/api/java/io/InvalidClassException.html
Can you check to see when this file was produced and then if possible locate the version of the SSAParser library at the time of it's creation?

Why is WSDL parser still importing external documents?

I tried to turn off importing documents in WSDL4J (1.6.2) in the way suggested
by the API documentation:
wsdlReader.setFeature("javax.wsdl.importDocuments", false);
In fact, it stops importing XML schema files declared with wsdl:import tag, but does stop importing files declared with xs:import tags.
The following code snippet [see at the end of the letter] for the example file
http://www.ibspan.waw.pl/~gawinec/example.wsdl
returns the following exception:
javax.wsdl.WSDLException: WSDLException (at /definitions/types/xs:schema):
faultCode=OTHER_ERROR: An error occurred trying to resolve schema referenced
at 'EchoExceptions.xsd', relative to
'http://www.ibspan.waw.pl/~gawinec/example.wsdl'.:
java.io.FileNotFoundException: This file was not found:
http://www.ibspan.waw.pl/~gawinec/EchoExceptions.xsd
at com.ibm.wsdl.xml.WSDLReaderImpl.parseSchema(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.parseSchema(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.parseTypes(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.parseDefinitions(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(Unknown Source)
at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(Unknown Source)
at IsolatedExample.main(IsolatedExample.java:15)
Caused by: java.io.FileNotFoundException: This file was not found:
http://www.ibspan.waw.pl/~gawinec/EchoExceptions.xsd
at com.ibm.wsdl.util.StringUtils.getContentAsInputStream(Unknown Source)
... 10 more
Can you suggest me any solution to this problem? I just don't want to import
external XML schemata.
Regards,
Maciej
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
public class IsolatedExample {
public static void main(String[] args) {
WSDLFactory wsdlFactory;
try {
wsdlFactory = WSDLFactory.newInstance();
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
wsdlReader.setFeature("javax.wsdl.verbose", false);
wsdlReader.setFeature("javax.wsdl.importDocuments", false);
wsdlReader.readWSDL("http://www.ibspan.waw.pl/~gawinec/example.wsdl");
} catch (WSDLException e) {
e.printStackTrace();
}
}
}
A quick look at WSDL4J (it's been a while since I've worked directly with this project) suggests that there is no option specifically to prevent the reading of imported schemas. You may have stumbled upon on a bug in WSDL4J's mechanism of deserializing schemas. That said, if you're not interested in the contents of any schemas, including those inlined in the WSDL document, you can register your own extension registry (simply modify the PopulatedExtensionRegistry class to leave out the SchemaDeserializer).
Specifically, leave out the following lines:
mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
SchemaImpl.class);
registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
new SchemaDeserializer());
registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
new SchemaSerializer());
mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
SchemaImpl.class);
registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
new SchemaDeserializer());
registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
new SchemaSerializer());
mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
SchemaImpl.class);
registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
new SchemaDeserializer());
registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
new SchemaSerializer());
I haven't used Java for webservices, but have you tried setting an absolute path to the schemas you import? Perhaps it's trying to load a local file.
You could also try sniffing the wire to see if you're making a request, perhaps it's malformed.
$0.02

Categories

Resources