0

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.

  • I wouldn't have expected it to; packages are folder structures, and your files aren't in a folder structure. [This question](http://stackoverflow.com/q/12117680/1079354), while not *exactly* a duplicate, can get you past the initial pain. – Makoto Jun 05 '14 at 01:55
  • I think you need to specify the classpath. ie. use "-cp ." If that does not work, play around with the -cp argument. – Braden Steffaniak Jun 05 '14 at 03:07

1 Answers1

-1

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()));
    }
Sumit Tyagi
  • 431
  • 2
  • 14
  • ...Why would you feel the need to do this? – Makoto Jun 05 '14 at 02:52
  • It is an alternative as I also written in first line of answer. It is also doing the same thing compiling a java source file. – Sumit Tyagi Jun 05 '14 at 02:59
  • Yes, I'm familiar with `JavaCompiler`. However, that's not what the question is asking, nor is it really helpful for this particular issue. – Makoto Jun 05 '14 at 03:07
  • SO you are trying this as second approach to compile the java file. I thought you stuck in compiling java file so suggested that alternative solution. I am sorry for that :) – Sumit Tyagi Jun 05 '14 at 03:09