1

I am creating java files from json Objects using a library called jsonschema2pojo-core.jar. It successfully creates the required files for me. Now I need to access the newly(dynamically) created file and creates its instance to use it further.

But as the newly created class is still not in the classpath I am unable to do this. Tried to do my part of research and figured out that Eclipse jars allows such refresh only in plugin projects. Can anyone suggest some thing for this?

public static void main(String[] args){


    String fileName = "MyJavaFile";
    POJOBuilder pojo = new POJOBuilder();
    pojo.buildPOJO("file:///C:/mypath/myJSON.json", fileName); //generates the java file MyJavaFile.java
    Object obj = null;
    try {


        obj = Class.forName("com.mypackage."+fileName).newInstance(); // Java file not available yet 
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

Can this be done through threads? I mean wait until the creation of the POJO is done and then start with the rest after that?

Madi
  • 55
  • 1
  • 8
  • If you want to load classes from a new jar you will need to use `URLClassLoader` - for example see http://stackoverflow.com/q/9691855/2670892 – greg-449 Nov 18 '14 at 07:42
  • Tried that way too. Was wondering if that can be achieved through threads? I have updated my question above. – Madi Nov 19 '14 at 15:27
  • No it can't be done with threads. Any new jar that is created is not on the classpath and can only be accessed using a new class loader. – greg-449 Nov 19 '14 at 15:46
  • Its not a jar that is being created. Its just a POJO. – Madi Nov 19 '14 at 16:26
  • If it is just a Java source file you will have to get it compiled. – greg-449 Nov 19 '14 at 16:29

0 Answers0