Sefi Erlich
Sefi Erlich

Reputation: 161

compile java in a different path from java

it's probably a stupid question but i need help.(i tried to solved it myself for 3 hours , so please do not block me) i'm trying to compile java files in a different directory.

i'm getting a folder with some .java files and i need to compile them. with:

public boolean complie() throws Exception{

    Process pro = Runtime.getRuntime().exec("javac -cp "+location+"/*.java");
     String line = null;
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(pro.getErrorStream()));
                while ((line = in.readLine()) != null) {
                    System.out.println(name + " " + line);
                }
}

but i'm getting errors. the errors are point to the usage of other class in the folder. (error: cannot find symbol)

when i'm trying to compile in the CMD after navigating to the folder with "javac *.java", there is no errors.

please help me!


update:

i have trid:

File pathToExecutable = new File(location );
    ProcessBuilder builder = new ProcessBuilder( pathToExecutable.getAbsolutePath(),"javac *.java");
    builder.directory( new File( location ).getAbsoluteFile() ); // this is where you set the root folder for the executable to run with
    builder.redirectErrorStream(true);
    Process process =  builder.start();

    Scanner s = new Scanner(process.getInputStream());
    StringBuilder text = new StringBuilder();
    while (s.hasNextLine()) {
      text.append(s.nextLine());
      text.append("\n");
    }
    s.close();

but getting CreateProcess error=5, Access is denied error (i'm running my IDE as administrator)

Upvotes: 0

Views: 321

Answers (1)

Carlitos Way
Carlitos Way

Reputation: 3424

One, see this: link ... (I believe that your executing a folder and not a commnad at the example given by you)

Two, ProcessBuilder should be called like this:

String classpath = "somePath" + File.pathSeparator + "otherpath";
ProcessBuilder builder = new ProcessBuilder("javac", "-cp " + classpath, "*.java");
builder.directory(new File(location));

This assuming that location contains the files .java that you want to compile...

UPDATE: This a little example that works for compiling and executing:

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class Compile {

    public static void main(String[] args) throws IOException {
        ProcessBuilder builder = new ProcessBuilder("javac", "hello/*.java");
        builder.directory(new File("C:\\Users\\carlitos\\Desktop"));

        Process pro = builder.start();
        String line = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(pro.getErrorStream()));

        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();

        // executing...
        ProcessBuilder builder1 = new ProcessBuilder("java", "hello.Main", "carlitosWay");
        builder1.directory(new File("C:\\Users\\carlitos\\Desktop"));

        Process pro1 = builder1.start();
        in = new BufferedReader(new InputStreamReader(pro1.getInputStream()));

        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }
}

And the main class:

package hello;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello: " + args[0]);
    }
}

My example assumes that at "C:/Users/carlitos/Desktop", there is a folder called: "hello", and it contains the class "Main"...

Upvotes: 1

Related Questions