Newbie confused with custom class loader tutorial

V

voger

Hello everyone

Tonight i ran into this
http://devcentral.iticentral.com/articles/Java/default.php (it requires
login). It has one tutorial about using custom class loaders.

My environment is Eclipse ide 3.1 which runs with java1.5 configured to
use j2sdk1.4.2_09.

As tutorial suggests i create the file structure as below (with a little
modification)

FileClassLoader
|-bin
|-src
|- FileClassLoader.java
|- MainApp.java
|- ITest.java
|- classes (sub-directory)
|- ITest.java
|- Test.java


This works fine. The .class files are compiled in the bin/ directory and
the application runs. But i can't see any files in the classes/
subdirectory. So why is this subdirectory there anyway?

I tried to move files ITest.java and Test.java into classes/
subdirectory and updating the appropriate imports and package
statements. Now the files are compiled ok under the correct subdirectory
but i get an output like

Class: Test
Loading...
Path: classes/Test.class
Exception: classes/Test.class (No such file or directory) not found.
null
Exception: null

No matter what i tried the error message is almost the same.

If i try to run it outside the Ide i get

Class: Test
Loading...
Path: classes/Test.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong
name: classes/Test)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.lang.ClassLoader.defineClass(ClassLoader.java:448)
at FileClassLoader.loadClass(FileClassLoader.java:98)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at MainApp.main(MainApp.java:33)


If I create a copy of these files under the src/ (with appropriate
modification in package statements) it works ok again. But when i double
click the class file under the classes/ subdirectory it displays the
copy without
"package classes;" statement even if the path in the directory tree is
correct (Eclipse pops up a bubble with the
path of the file).

Can someone please explain why this is happening? And how can i group
all the classes that i want to load with
the custom class loader in one separate package (sub folder or folder
somewhere else)?
 
R

Roedy Green

This works fine. The .class files are compiled in the bin/ directory and
the application runs. But i can't see any files in the classes/
subdirectory. So why is this subdirectory there anyway?

Some people like to put class files in the same directory as the
source, others in a separate directory.

Presumably there is a place somewhere in Eclipse where you can set the
-d targetdir. I could not find it on a cursory search though I
vaguely remember telling Eclipse at some point to put the class files
in the same place as the source files. Perhaps it asks you that when
you start a new project or package.
 
C

Chris Uppal

voger said:
Tonight i ran into this
http://devcentral.iticentral.com/articles/Java/default.php (it requires
login). It has one tutorial about using custom class loaders.

My environment is Eclipse ide 3.1 which runs with java1.5 configured to
use j2sdk1.4.2_09.

I haven't looked into the details of your problem (which is undoubtedly Eclipse
specific), but I suggest that if you are playing with classloaders then it is a
/bad/ idea to use an IDE such as Eclipse. The problem is that it "thinks" that
when you compile something, that's because you want your program to be able to
find the classes as it runs normally, but that's exactly what you /don't/ want
to have happen (or your classloader will not be used).

I strongly advise you to copy your .java files out of the Eclipse area (by all
means use Eclipse as your editor to create them, if you wish), use javac (from
the JDK) to compile them, and use java -cp <whatever> to run them. If you
don't yet know how to do that, then you'll need to learn before you'll be able
to make much sense of classloaders anyway.

-- chris
 
P

pkriens

The bin directory is defined in the Eclipse project properties "Java
Build Path" Source, default output folder.

But it looks like your program expects the classes to be on the
classpath. I suspect there is a java -classpath classes in the
documentation? This runtime classpath is defined in the Run dialog.
Just create a Run or Debug launch file for your example and add the
classes directory to your Classpath (special tab). Add it to the user
entries.

Unless your code has a special Classloader that needs the classes
directory as a parameter?
 
A

Abhijat Vatsyayan

pkriens said:
The bin directory is defined in the Eclipse project properties "Java
Build Path" Source, default output folder.

But it looks like your program expects the classes to be on the
classpath. I suspect there is a java -classpath classes in the
documentation? This runtime classpath is defined in the Run dialog.
Just create a Run or Debug launch file for your example and add the
classes directory to your Classpath (special tab). Add it to the user
entries.

Unless your code has a special Classloader that needs the classes
directory as a parameter?
The FileClassLoader assumes that there is a classes directory (inside
the current working directory of the java process) inside which all the
class files are located. Eclipse I think will put all classes to a "bin"
(or some other user defined) output directory. This will explain the
first error message.

The second error message looks cryptic but heres a guess -
looks like the classloader is being asked to resolve a class named
"Test" using bytes that define a class named classes.Test (notice the
package name "classes") . I have a feeling the you somehow asked eclipse
to "fix errors" (or something similar) and eclipse changed the package
declaration in Test class and made it part of package named "classes".
Is there a package declaration in your "Test.java" file (hopefully you
have only one of this around!)

Abhijat
 
V

voger

Abhijat said:
The FileClassLoader assumes that there is a classes directory (inside
the current working directory of the java process) inside which all the
class files are located. Eclipse I think will put all classes to a "bin"
(or some other user defined) output directory. This will explain the
first error message.

Well Eclipse will put all classes under a bin directory and create
subdirectories
according to package declaration of each file. And yes there can be
complex classpath
in Eclipse. Today i added both src/ and src/classes/ in the classpath.
The result was to put
all the .class files under the /bin directory which seems logical
because now all the files belong
to default package
The second error message looks cryptic but heres a guess -
looks like the classloader is being asked to resolve a class named
"Test" using bytes that define a class named classes.Test (notice the
package name "classes") . I have a feeling the you somehow asked eclipse
to "fix errors" (or something similar) and eclipse changed the package
declaration in Test class and made it part of package named "classes".
Is there a package declaration in your "Test.java" file

Eclipse didn't fix any errors. I added the package classes; declaration
myself because i
had only the src/ folder in my classpath. Bad idea...

(hopefully you
have only one of this around!)
Ummm... I made many combinations and some of them had two of this but i
guess
only the right one worked anyway :D

Today i tried to compile from pure command line. So i used:
javac Test.java -d classes/
javac MainApp.java
java MainApp

and it worked so i guess Eclipse's auto builder is out of the question
until i learn how to do
-d. I guess i should be using ant for that.

Thank you for all the replies
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top