Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:53:49+00:00 2026-05-30T15:53:49+00:00

Hi I have requirement to create ,compile and load java classes run time. Using

  • 0

Hi I have requirement to create ,compile and load java classes run time. Using FTL i am creating java source files , and able to compile the source if there is no dynamic dependency.

To elaborate with an instance, I have two java source file, one interface and its implementation class. I am able to compile the interface using java compiler api as follows

String classpath=System.getProperty("java.class.path");
        String testpath =classpath+";"+rootPath+"/lib/is_wls_client.jar;"+rootPath+"/rtds_wls_proxyclient.jar;.;";
        File javaFile =  new File(javaFileName+".java");
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        List<String> optionList = new ArrayList<String>();
        optionList.addAll(Arrays.asList("-classpath",testpath));
        StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null);
        Iterable fileObjects = sjfm.getJavaFileObjects(javaFile);
        JavaCompiler.CompilationTask task = compiler.getTask(null, null, null,optionList,null,fileObjects);
        task.call();
        sjfm.close();

I set class path for static classes which are already in the classpath , but this approach do not work for dynamically created classes? Any custom class loader will do the fix? My final implementation will be in web/app server

Any feedback will be highly appreciated

Satheesh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:53:50+00:00Added an answer on May 30, 2026 at 3:53 pm

    I was able to solve this issue by compiling all the java files together. Using FTL I generate the java classes, and then compile it using java compiler api and load classes with custom class loader

    Java Complier

    private  void compile(File[] files) throws IOException{
            String classpath=System.getProperty("java.class.path");
            String rootPath=getServletContext().getRealPath("/");
            System.out.println("--> root Path "+rootPath);
            String testpath=classpath+";.;xx.jar;yy.jar";
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            List<String> optionList = new ArrayList<String>();
            optionList.addAll(Arrays.asList("-classpath",testpath));
    //      optionList.addAll(Arrays.asList("-d",rootPath+"/target"));
            StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null);
            Iterable fileObjects = sjfm.getJavaFileObjects(files);
            JavaCompiler.CompilationTask task = compiler.getTask(null, null, null,optionList,null,fileObjects);
            task.call();
            sjfm.close();
    
        }
    

    Below code snippet shows how to use custom class loader

    class CustomClassLoader extends ClassLoader {
    
         public CustomClassLoader(ClassLoader parent) {
                super(parent);
         }
    
        public Class findClass(String className,String path) {
            byte[] classData = null;
            try {
                FileInputStream f = new FileInputStream(path);
                int num = f.available();
                classData = new byte[num];
    
                f.read(classData);
            } catch (IOException e) {
                System.out.println(e);
            }
            Class x = defineClass(className, classData, 0, classData.length);
            return x;
        }
    }
    

    thanks
    Satheesh

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI, I have a requirement to create instance for list object at runtime using
I have a requirement to create a simple database in Access to collect some
I have a requirement to create a windows form control which has to detect
I have a requirement to create a simple windows forms application that allows an
I have a requirement to create two different maps in C++. The Key is
If I have a requirement to create a data structure that has the following
I am new to MySQL coming from Oracle. I have a requirement to create
I have a requirement to take client side XAML (from Silverlight) and create a
I have a requirement in sharepoint where I want to Create the top link
Normally I create web application projects and use code-behind, but I have a requirement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.