Results 1 to 2 of 2
  1. #1
    SteamIDHere is offline Member
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Question Load a class from the src folder

    Within my program I allow users to write their own code to be executed. I then save that code in a .java file and then compile. This gives me a .java file and a .class file within the same package in the src folder. However, the compilation process also creates a .class file in the build folder. When the user edits their code and then re-runs it, the .class file in the build folder does not get updated with the new data, but the .class in the src folder does.

    What I want to do is to execute the .class file in the src folder and not the build folder. So far I have the following method, but that somehow refers to the .class file in the build folder:

    public void runMethod(String packageFilePath, String className){

    try{

    URI fileURI = new URI("file:/src/uk/learningAid/UserInputs/");
    URL url = fileURI.toURL();
    URL[] urls = new URL[]{url};

    ClassLoader classLoader = new URLClassLoader(urls);

    Class theUserClass = classLoader.loadClass("uk.learningAid.UserInputs." +className);

    SecurityManager adminSM = System.getSecurityManager();
    System.setSecurityManager(new UserSecurityManager());

    classObject = theUserClass.newInstance();

    Method userMethod = theUserClass.getMethod("userMethod");

    try{
    userMethod.invoke(classObject, null);
    }catch(InvocationTargetException exc){
    exc.printStackTrace();
    }

    System.setSecurityManager(adminSM);
    }
    catch(Exception e){
    e.printStackTrace();
    }
    }


    The method which I use to compile the .java class is as follows:

    public boolean compileClass(String pathToFile, String userClassName){

    int compilationResult = compiler.run(null, null, null, pathToFile+userClassName+".java");

    if(compilationResult == 0){
    System.out.println("Compilation Successful");

    return true;
    }else{
    System.out.println("Compilation Failed");
    return false;
    }

    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Load a class from the src folder

    You need to understand how classloaders work, particularly how they delegate to their parents. Google it. Then you'll understand the rest of this comment.

    You need to make sure the compiled class file exists only in a location that is not on the system classloader's classpath. Then you can load the class from your custom classloader without the system classloader loading it first. Whenever you recompile the class, destroy all references to the custom classloader, the dynamically loaded class, and all instances of it.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Error: Could not find or load main class Test.class
    By java_aim in forum New To Java
    Replies: 11
    Last Post: 08-13-2013, 05:05 PM
  2. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  3. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  4. Importing a class folder
    By malaguena in forum Advanced Java
    Replies: 6
    Last Post: 03-15-2011, 12:04 PM
  5. how to use class in upper folder
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:55 PM

Tags for this Thread

Posting Permissions

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