0

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)

4
  • Maybe the problem is the working directory. Are you running the java program in the same CMD window? Commented May 18, 2016 at 19:48
  • Where is location defined? Commented May 18, 2016 at 19:49
  • It may be the location, you should print location just to be sure. And if it is a relative location, try printing the result of something like exec("pwd") Commented May 18, 2016 at 19:52
  • @FedericoNafria , exec("pwd") throws "Cannot run program "pwd": CreateProcess error=2, The system cannot find the file specified" Commented May 18, 2016 at 19:56

1 Answer 1

1

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"...

Sign up to request clarification or add additional context in comments.

2 Comments

hi... i't helped (to compile), but now i'm trying to run the java program that i just compiled with the same code. 'ProcessBuilder builder = new ProcessBuilder("java " ," ex1"); builder.directory(new File(location));' the class path is the path to all class and packages. but getting 'Error: Could not find or load main class ex1'
Do the classes declared a package... Example: package dummy; ... public class ex1 ?? IF so, builder.directory() should be called with an upper level about "location" in the FileSystem Tree ... and then, new ProcessBuilder("java" , "dummy.ex1") ... I will add an example that works for me... (Also, please vote for my answer, It resolve your question)....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.