Newbie confused with custom class loader tutorial

Discussion in 'Java' started by voger, Sep 19, 2005.

  1. voger

    voger Guest

    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)?
     
    voger, Sep 19, 2005
    #1
    1. Advertising

  2. voger

    Roedy Green Guest

    On Tue, 20 Sep 2005 01:08:43 +0300, voger <> 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.
     
    Roedy Green, Sep 20, 2005
    #2
    1. Advertising

  3. voger

    Chris Uppal Guest

    voger wrote:

    > 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
     
    Chris Uppal, Sep 20, 2005
    #3
  4. voger

    pkriens Guest

    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?
     
    pkriens, Sep 20, 2005
    #4
  5. 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
     
    Abhijat Vatsyayan, Sep 20, 2005
    #5
  6. voger

    voger Guest

    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 :D

    > 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
     
    voger, Sep 20, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    586
    Andrea Desole
    Dec 14, 2005
  2. spaceman

    Custom class loader

    spaceman, Sep 25, 2006, in forum: Java
    Replies:
    3
    Views:
    3,044
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 30, 2006
  3. =?iso-8859-1?q?Andr=E9_Wagner?=

    Loading a class using the applet class loader

    =?iso-8859-1?q?Andr=E9_Wagner?=, May 30, 2007, in forum: Java
    Replies:
    7
    Views:
    457
    Mark Space
    May 31, 2007
  4. Karsten Wutzke
    Replies:
    0
    Views:
    404
    Karsten Wutzke
    Jun 29, 2007
  5. gorudonu
    Replies:
    0
    Views:
    1,070
    gorudonu
    Aug 5, 2010
Loading...

Share This Page