Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Newbie confused with custom class loader tutorial

Reply
Thread Tools

Newbie confused with custom class loader tutorial

 
 
voger
Guest
Posts: n/a
 
      09-19-2005
Hello everyone

Tonight i ran into this
http://devcentral.iticentral.com/art...va/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 :44
at FileClassLoader.loadClass(FileClassLoader.java:9
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 35)
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)?
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2005
On Tue, 20 Sep 2005 01:08:43 +0300, voger <(E-Mail Removed)> wrote or
quoted :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      09-20-2005
voger wrote:

> Tonight i ran into this
> http://devcentral.iticentral.com/art...va/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



 
Reply With Quote
 
pkriens
Guest
Posts: n/a
 
      09-20-2005
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?

 
Reply With Quote
 
Abhijat Vatsyayan
Guest
Posts: n/a
 
      09-20-2005
pkriens wrote:
> 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
 
Reply With Quote
 
voger
Guest
Posts: n/a
 
      09-20-2005
Abhijat Vatsyayan wrote:
> pkriens wrote:
>
>> 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.


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

> Abhijat


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Class Loader for Web Application using Tomcat tapas.adhikary Software 0 04-22-2008 08:53 AM
Library class loader (setting system class loader parent) Karsten Wutzke Java 0 06-29-2007 04:44 AM
Loading a class using the applet class loader =?iso-8859-1?q?Andr=E9_Wagner?= Java 7 05-31-2007 07:51 PM
Custom class loader spaceman Java 3 09-30-2006 07:28 PM
(jsp) class loader not finding generated JSP class unixfreak0037@gmail.com Java 1 12-14-2005 09:35 AM



Advertisments