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
Related
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
I am working along with the ATM Case Study from Deitel java how to program 9th edition.
The case study is at chapter 13, page 546(in case someone has the book and would like to check),I am sure my code is 100% as the book suggested.
I have all the code set but when I try to run the program it is giving me this:
Error: Could not find or load main class come.example.atm.AtmRun
when I tried to compile the class by using terminal from the class path it gave me this error:
localhost:atm user$ javac AtmRun.java
AtmRun.java:5: error: cannot find symbol
Atm theATM = new Atm();
^
symbol: class Atm
location: class AtmRun
AtmRun.java:5: error: cannot find symbol
Atm theATM = new Atm();
^
symbol: class Atm
location: class AtmRun
2 errors
this is the class am running: straight forward but I cant seem to find the problem. any help?
package come.example.atm;
public class AtmRun {
public static void main (String[] args){
Atm theATM = new Atm();
theATM.run();
}
}
UPDATE: when i run the .class file from the bin directory of project using command java AtmRun i get this:
Exception in thread "main" java.lang.NoClassDefFoundError: AtmRun (wrong name: come/example/atm/AtmRun)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Class Atm has a constructor Atm() and public void run() along with other methods, the class is big so i think its better if I don't post the code it however you can check in the book if you can.
Note: I am using eclipse, other projects and classes work and run properly.
for me it worked. Please follow below steps:
Y:\HashmiAb\Desktop\Trash\test>javac come\example\atm\Atm.java
Y:\HashmiAb\Desktop\Trash\test>javac come\example\atm\AtmRun.java
Y:\HashmiAb\Desktop\Trash\test>java come.example.atm.AtmRun
Heloo
It matters how you use -d and -cp options of javac and java commands. I didn't use any of this options.
For more help please find the directory structure.
+test
-+come
-+example
-+atm
-AtmRun.java
-Atm.java
Thanks.
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%.
I have built my own custom AWT classes in my home folder in java_src/classes.
Each of the java files contain the package classes; declaration at the top.
I also created a sample program called ScreenDemo.java and placed it in the java_src/ folder to use the custom AWT classes instead of java.awt.
//ScreenDemo.java
import classes.Screen;
class ScreenDemo
{
public static void main(String args[])
{
Screen.init(20,15,3);
}
}
But when i attempt to compile ScreenDemo.java,an error is displayed
java_src/ScreenDemo.java:1: package classes does not exist
import classes.Screen;
^
java_src/ScreenDemo.java:6: cannot find symbol
symbol : variable Screen
location: class ScreenDemo
Screen.init(20,15,3);
^
2 errors
When i add the path i encounter this error
Exception in thread "main" java.lang.NoClassDefFoundError: ScreenSample (wrong name: classes/ScreenSample)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: ScreenSample. Program will exit.
While compiling the code the compiler is complaining about the missing class Screen.java which i assume is inside "classes" package.
Either you need to add src\classes\ to the src folder list or move the Screen.java to the same location as ScreenDemo.java and then try to compile again.
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?