简体   繁体   中英

compile a java class with package name in another java class file by using javac

I am trying to compile a java class file in another java class file by using javac command. It went well if these two file do not have any package name with them.

Class Laj

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Laj {

      private static void printLines(String name, InputStream ins) throws Exception {
        String line = null;
        BufferedReader in = new BufferedReader(
            new InputStreamReader(ins));
        while ((line = in.readLine()) != null) {
            System.out.println(name + " " + line);
        }
      }

      private static void runProcess(String command) throws Exception {
        Process pro = Runtime.getRuntime().exec(command);
        printLines(command + " stdout:", pro.getInputStream());
        printLines(command + " stderr:", pro.getErrorStream());
        pro.waitFor();
        if(pro.exitValue() != 0){
            System.out.println(command + " exitValue() " + pro.exitValue());    
        }       
      }

      public static void main(String[] args) {
        try {
          runProcess("javac simpleTest.java");
          runProcess("java simpleTest");
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

Class SimpleTest

public class simpleTest {
    public static void main(String[] args) {
        System.out.println("What's wrong with it");
    }
}

I can use the commands javac Laj.java and java Laj to compile and run them well. However if I add the package name, for example package compileTest in the front of these two classes and modify the runProcess part of the code in Laj to

runProcess("javac -d . compileTest.simpleTest.java");
runProcess("java compileTest.simpleTest");

the code would not work.

Can anyone help me, thank you.

Why do not you use 'JavaCompiler' class to compile your java file. Please see below example I have compiled a java class with package name.

Package Name = com.main
Class Name = MainClass.java
Source Dir = src

public  void compileClass() {
        System.setProperty("java.home", "G:\\Java\\Tools\\installed\\JDK");   // Set JDK path it will help to get compiler
        File root = new File("/src");   // Source Directory 
        File sourceFile = new File(root, "com/main/MainClass.java");    // Java file name with package 
        sourceFile.getParentFile().mkdirs();
        try {
            new FileWriter(sourceFile).close();  // Read Java file
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        System.out.println(compiler.run(null, null, null, sourceFile.getPath()));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM