1

I'm writing a program that includes a feature where the user can type in Java code into a text box and be able to compile and run it. The error I get is: enter image description here

The two directories shown at the top are correct, and the command works when I do it manually through command prompt from the same working directory. I'm using Windows 10, and also here's the code:

public Process compile() throws IOException { save(); //saves changes to source file System.out.println(file.getCanonicalPath()); ProcessBuilder processBuilder = new ProcessBuilder("javac", file.getCanonicalPath()); processBuilder.directory(new File(settingsFile.getJdkPath())); System.out.println(processBuilder.directory()); Process process = processBuilder.start(); //Throws exception this.compiledFile = new File(file.getParentFile(), file.getName().replace(".java", ".class")); return process; } 

File to compile: enter image description here

Working directory: enter image description here

7
  • check the file permissions of javac Commented Dec 2, 2018 at 18:58
  • @flakes: the error message does not suggest that this is the problem Commented Dec 2, 2018 at 19:08
  • @flakes I added some debugging code, and the application does have permission to execute javac. I even went one step further and granted all applications full control of javac (probably a bad idea, but will change it back later). Commented Dec 2, 2018 at 19:09
  • java doesn't infer file extensions like windows does, maybe you want 'javac.exe' Commented Dec 2, 2018 at 19:09
  • @4dc0 Just tried it, and I still get the same error. The only difference now is that it says "cannot run program javac.exe" instead of "cannot run program javac". Commented Dec 2, 2018 at 19:11

1 Answer 1

2

Using this code, I was able to compile a Test.java file into a Test.class file on my Desktop.

import java.io.IOException; public class App { public static Process compile() throws IOException { String myFilePath = "C:\\Users\\redacted\\Desktop\\Test.java"; String javacPath = "C:\\Program Files\\Java\\jdk1.8.0_171\\bin\\javac.exe"; ProcessBuilder processBuilder = new ProcessBuilder(javacPath, myFilePath); return processBuilder.start(); } public static void main(String[] args) throws IOException { Process process = compile(); } } 

Capture

Using String javacPath = "javac.exe"; also worked, but that could be because my JDK bin is on my PATH variable.

There is something wrong with your paths or permissions in the ProcessBuilder constructor call.

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

1 Comment

Thank you! The problem was that I still had to specify the path of javac.exe in the command itself even though I had it set as the working directory in ProcessBuilder.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.