How can I integrate Java Compiler in Android Studio Application

Viewed 26

I was trying to integrate JAVA compiler into my application but was failing everytime.

Below is my code which i tried testing but it too failed

    Looper.prepare();
    StringBuilder errors = new StringBuilder();
    PrintWriter writer =
            new PrintWriter(
                    new OutputStream() {
                        @Override
                        public void write(int p1) throws IOException {
                            errors.append((char) p1);
                        }
                    });

    Main main = new Main(writer, writer, false, null, null);

    Main.Logger logger = main.logger;

    File output = new File(outputPath);
    if(!output.exists()) output.mkdirs();
    final ArrayList<String> args = new ArrayList<String>();

    args.add("-classpath");
    args.add("[WHAT TO ADD HERE]");
    args.add("-log");
    args.add(outputPath+"/logz.xml");
    args.add("-g");
    args.add("-1.8");
    args.add("-d");
    args.add(output.getAbsolutePath());
    StringBuilder classpath = new StringBuilder();
    if (classpath.length() > 0) {
        args.add("-cp");
        args.add("[WHAT TO ADD HERE]");
    }
    args.add("-proc:none");
    args.add("-sourcepath");
    args.add(" ");
    args.add(javaPath);

    String[] argsArry = new String[args.size()];
    for (int i=0; i<args.size(); i++){
        argsArry[i] = args.get(i);
    }

    main.compile(args.toArray(new String[0]));

    if(main.log.isEmpty()){
        main.log = "EMPTY";
    }

    if (main.globalErrorsCount > 0 | !output.exists() | output.listFiles().length != 0) {
        compilationListener.compilationFailed(main.log, errors.toString());
    }else{
        compilationListener.compilationSuccess(main.log);
    }

Here the compiler I am trying to add compiles all the java files which are present in the src directory

0 Answers
Related