0
votes

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.

1
I wouldn't have expected it to; packages are folder structures, and your files aren't in a folder structure. This question, while not exactly a duplicate, can get you past the initial pain. - Makoto
I think you need to specify the classpath. ie. use "-cp ." If that does not work, play around with the -cp argument. - Braden Steffaniak

1 Answers

-1
votes

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()));
    }