Results 1 to 6 of 6
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default Class.forName Exception

    Hi, I've ran into a problem with trying to load a class up. I can't find anything regarding how to appropriately load in a class given a specific location.


    Heres what Eclipse is telling me:

    Exception in thread "main" java.lang.ClassNotFoundException: C:\Users\xxxx\workspace\testing\bin\battlebots\pla yerClass.class
    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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at battlebots.Main.main(Main.java:29)

    I'm attempting:

    Java Code:
    		String fileName="C:\\Users\\xxxx\\workspace\\testing\\bin\\battlebots\\playerClass.class";
    		Class firstPlayer = Class.forName(fileName);
    		player test1 = (player) firstPlayer.newInstance();
    It throws the error at Class firstPlaye = Class.forName(fileName);

    I was hoping I could give it a specific location of the class. Even if I can I'm not sure if I need to specific .class on the end of the file or just leave it be. It has to be a silly mistake I'm making, I'll keep trying while I wait for a post.

    Thanks for any help

  2. #2
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Looks like I found a soltuion.

    using "battlebots.playerClass" found the class and loaded it. But what if the class I want isn't in the package?

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    ClassLoader works off the classpath, not OS file system paths. Make sure the package containing the class is on the classpath before starting Java.

  4. #4
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Is there no other solution to this? The class has to be on the Classpath period? I've tried to move the file I'm working with (after downloading it) inside the classpath but there appears to be a Bug with Java and renameTo() on windows regarding if your workspace is in use or if its in the My Documents directory or something.

    Any other solutions?

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Terminology suggests confusion about what a classpath is. It's a list of paths that tells the classloaders where to look for classes and is usually set by whatever starts up the JVM (using environment variable/html attrib). If a file you want to get at isn't on the classpath, then move the file or change the classpath.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    This is core functionality in Java. It serves the same function the path serves in the OS. The classpath concept provides flexibility in how the JVM finds classes. It allows a patch directory to be searched for a class before the production directory, and it allows support for JAR files.

    The classpath has clear rules. If you don't understand them, then look at the Sun tutorial. Your Java apps will not work if the classpath is not properly set.

Similar Threads

  1. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM
  2. How to create your own Exception class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM
  3. Replies: 1
    Last Post: 03-14-2008, 02:51 PM
  4. about Class.forName
    By angus203 in forum New To Java
    Replies: 0
    Last Post: 11-25-2007, 04:47 AM
  5. question about Class.forName()
    By oregon in forum JDBC
    Replies: 4
    Last Post: 08-01-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •