Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Norm

Thread: URLClassLoader with wampserver problem ClassNotFoundException

  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default URLClassLoader with wampserver problem ClassNotFoundException

    Hi im getting problem here this is the code what im trying i just copy this code from some where in google.

    first this code work if i run Main.java the output will be "MessageDialog(null,"This is Method from url")" from the Test.java but if i try to cut the Test.class in "build\classes\Test.class" and paste it into "C:\wamp\www\DIR\Test.class" i get some error whats wrong with this code?
    seems like it cant find the Test.class file.

    im using netbeans and wampserver.

    Java Code:
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLClassLoader;
    
    public class Main {
    
    	public static void main(String[] args) throws MalformedURLException,
    			ClassNotFoundException, InstantiationException,
    			IllegalAccessException, SecurityException, NoSuchMethodException,
    			IllegalArgumentException, InvocationTargetException {
                URL[] urls = new URL[] {new URL("http://localhost/DIR/Test")};
       // URL[] urls = new URL[] {new URL("http://localhost/DIR/Test.class")};
    		ClassLoader loader = new URLClassLoader(urls);
                    Class<?> cls = loader.loadClass("Test");
    		Object instance = cls.newInstance();
    		Method theMethod = cls.getMethod("ActionMethod");
    		theMethod.invoke(instance);
    	}
    }
    Java Code:
    import javax.swing.JOptionPane;
    public class Test {
        public static void main(String[]args){
            new Test().ActionMethod();
        }
        public void ActionMethod(){
            JOptionPane.showMessageDialog(null,"This is Method from url");
        }
    }
    Exception in thread "main" java.lang.ClassNotFoundException: Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    at Main.main(Main.java:16)
    Java Result: 1

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Is the Test.class file on the classpath when you execute your Main class?

    What server do you have executing as localhost?
    Last edited by Norm; 08-16-2011 at 04:51 PM. Reason: Added question about server

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'd imagine it was closed since this isn't your code; since you just copied it, I'm less likely to believe that you understand what is going on. Helping you may be hard if you don't understand what the code is doing. I tentatively unlocked the thread, however; it may be re-closed if deemed necessary.

  4. #4
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    it does not matter even i just copied it from some where because it is only a short code so i think i can understand the code but now i need help to resolve it.

    first i run my Main class and i works but then i remove the "Test.class" from "build\classes\Test.class" and put it into C:\wamp\www\DIR\Test.class and it doest not work and it gives me error.

    "What server do you have executing as localhost"
    my localhost is wampserver

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Does your server get an HTTP GET request for the file named: Test?
    Shouldn't the request be for Test.class?

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Does your server get an HTTP GET request for the file named: Test?
    Shouldn't the request be for Test.class?
    no . . actually i just put my Test.class in C:\wamp\www\DIR\ directory. i just want to do is to invoke the method from test.class it does not matter where ever i put my Test.class that ok if i put my test.class in C:\+path or D:\+path i just want to invoke a method from another class in different path.

    it is common to use multiple class in thesame directory of java package but now i try to put the other class into different directory for example "My Documents\NetBeansProjects\Test\src" is the source of my Main.java and Test.java then if i run it it works because Main.class can find the Test.class because they are both the same directory

    and here what i want to do i want that Main.class will look for Test.class in different directory like i said i remove the test.class in "build\classes\" directory and put it into another directory. and now the Main.class cant find the Test.class

    Thats why there is a code URL[] urls = new URL[] {new URL("http://localhost/DIR/Test") and i thought that it will works but it give me error i dont know how to fix it.
    Last edited by Jhovarie; 08-16-2011 at 11:17 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Did you read the API doc for the URLClassLoader class? It discusses the correct format for the URL when you want to get a class file from a directory. Your format is for another way to access class files.
    You should read it so you know how to use the URLClassLoader.
    Jhovarie likes this.

  8. #8
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    ah this one work it just a little modification

    Java Code:
     URL[] urls = new URL[] {new URL("file:/C:/wamp/www/DIR/")};

Similar Threads

  1. ClassNotFoundException
    By oontvoo in forum Java Applets
    Replies: 7
    Last Post: 05-14-2010, 12:09 AM
  2. ClassNotFoundException
    By Edward in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 08:08 PM
  3. ClassNotFoundException JSP
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2009, 08:41 AM
  4. ClassNotFoundException
    By Chezelle in forum Java Applets
    Replies: 5
    Last Post: 12-24-2008, 05:26 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
  •