-
please help with class not found issue
i am new to java and i am try to use serializable. i am learning from examples in a book i have and i have typed in couple java files:
first one
Code:
import java.io.*;
import java.util.*;
public class StudentList implements Serializable {
//Vector holding out students
ArrayList<String> list;
//default constructor
public StudentList() {
}
public void addStudent(String value) {
//add a string representing a student name
if ( value != null ) {
list.add(value);
}
}
public void listStudents() {
//iterate over list vector, printint all Strings
for ( int x = 0; x < list.size(); x++ ) {
System.out.println("Student " + x + " : " + (String)list.get(x));
}
}
}
this one i have updated a little due to some of the original file being depreciated.
i was getting warnings when i would compile it.....
hopeing i did this correct
the original was as follows
Code:
import java.io.*;
import java.util.*;
public class StudentList implements Serializable {
//Vector holding out students
Vector list = new Vector(6);
//default constructor
public StudentList() {
}
public void addStudent(String value) {
//add a string representing a student name
if ( value != null ) {
list.addElement(value);
}
}
public void listStudents() {
//iterate over list vector, printint all Strings
for ( int x = 0; x < list.size(); x++ ) {
System.out.println("Student " + x + " : " + (String)list.elementAt(x));
}
}
}
the second one is as follows, not modified....
Code:
import java.io.*;
public class StudentListApplication {
//Default Constructor
public StudentListApplication() {
}
//adds student names to list
public void buildStudentList(StudentList value) {
value.addStudent("Bob Robinson");
value.addStudent("Steve Bobinson");
value.addStudent("Rob Stevinson");
value.addStudent("Todd Thompson");
value.addStudent("Tom Toddson");
value.addStudent("Rob Bobinson");
}
// Stores the Serializable StudentList to the file "file.dat"
public void putStudentList(StudentList value) {
try {
//create the ObjectOutputStream passing it the FileOutputStream
//object that points to our persistent storage.
ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("file.dat"));
//write the StudentList to the ObjectOutputStream
os.writeObject(value);
os.flush();
os.close();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
public StudentList getStudentList() {
StudentList list = null;
try {
//create the ObjectInputStream passing it the FileInputStream object that points
//to out persistent storage.
ObjectInputStream is = new ObjectInputStream( new FileInputStream("file.dat"));
//Read the stored object and downcast it back to a StudentList
list = (StudentList)is.readObject();
is.close();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
catch (ClassNotFoundException ce) {
System.err.println(ce.getMessage());
}
return list;
}
public void invoke() {
StudentList list = new StudentList();
buildStudentList(list);
System.out.println("Before being serialized.");
list.listStudents();
putStudentList(list);
System.out.println("After being read back in.");
//get the StudentList and print it out
StudentList inList = getStudentList();
if ( inList != null ) {
inList.listStudents();
}
else {
System.err.println("readObject failed.");
}
try {
System.out.println("\n Press enter to quit.");
System.in.read();
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static void main(String[] args) {
StudentListApplication studentListApplication = new StudentListApplication();
studentListApplication.invoke();
}
}
the problem i have is when i try to compile the second one i get errors everywhere that the class StudentList is referenced...
errors are
StudentListApplication.java:11: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
public void buildStudentList(StudentList value) {
^
StudentListApplication.java:21: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
public void putStudentList(StudentList value) {
^
StudentListApplication.java:36: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
public StudentList getStudentList() {
^
StudentListApplication.java:37: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
StudentList list = null;
^
StudentListApplication.java:43: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
list = (StudentList)is.readObject();
^
StudentListApplication.java:57: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
StudentList list = new StudentList();
^
StudentListApplication.java:57: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
StudentList list = new StudentList();
^
StudentListApplication.java:67: cannot find symbol
symbol : class StudentList
location: class StudentListApplication
StudentList inList = getStudentList();
^
8 errors
both java files and the StudentList.class are in the same directoy on my machine....
any help would be great, thanks mike.......
-
You either need to make the StudentList class a part of your program file to be compiled along with the rest of the code, or you need to "import" StudentList. The compiler needs to be explicitly told where to find your definition of that class if you have not included that definition in the code being compiled.
If you were using an IDE (such as NetBeans or Eclipse) you would be working with a project and the project would manage these various class files.
-
still need help
i have tried this at the top with the other import statements
Code:
import StudentList;
and i get these errors
StudentListApplication.java:2: '.' expected
import StudentList;
^
StudentListApplication.java:2: ';' expected
import StudentList;
^
2 errors
i use bluefish....
and i compile from the command line with javac....
do not know how to point to StudentList other than with the import statement......
thanks mike.....
-
ok i got it, well i think so anyhow.....
well i tried to compile both source files at the same time and i got no errors and 2 class files.
i was compiling StudenList.java and then trying to compile StudentListApplication.java and was getting the errors.....
i tried this and it worked with out errrors:
javac StudentList.java StudentListApplication.java
hope this may help others,
mike
-
Well, I don't know why you were seeing this error, or why it is now corrected. The error you had indicates that the compiler couldn't find the file StudentList.class. If you had the filename misspelled or had some other typo, it could cause this.
I created (via copy and paste) and built both your files, one at a time with no error.
Note that as long as your classes are in the same package, no import statements are necessary. Also, as long as all your java files are in the same directory, you shouldn't need a classpath or have trouble finding the classes.
According to the compiler docs:
If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
So, be sure you are building from the directory containing the java files, or set your classpath accordingly.
What I think is most likely is that the StudentList.class file either wasn't there or had an error in it.
Whatever the problem was, building all the files at the same time shouldn't make a difference. What's more likely is that rebuilding StudentList fixed whatever was wrong with the class file and allowed StudentListApplication to build correctly.
Hope this helps.
Last edited by ajhampson; 06-10-2010 at 12:09 AM.
Reason: quick fix
-
not real sure
both files are in the same directory, and all i did different was compile them at the same time, i made no changes to either file......
dont really know why it worked this way and not the other........
-
Thank you it's how the community works.
Although I would add that unless there was an error during the transfer of files (in which case reinstalling would resolve it ) it should not be necessary to edit the core files to make the site work if the server meets the requirements (otherwise everyone would need to do it ie a bug and devs would be working to resolve it).
Similar Threads
-
By IronJack in forum Java
Replies: 12
Last Post: 08-08-2006, 06:52 PM
-
By vicky_31 in forum Java
Replies: 1
Last Post: 05-24-2006, 06:31 AM
-
Replies: 0
Last Post: 01-14-2006, 08:01 PM
-
Replies: 13
Last Post: 08-02-2005, 11:50 PM
-
Replies: 8
Last Post: 04-06-2005, 03:17 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|