2

I want my java program to do the following things:

Access cmd and execute the commands: "d:", "cd D:\Java Projects\imageProject", "screenshot-cmd"

I tried to google that and found some code examples but none of them worked because I probably have no idea what i'm doing.

This is what I have now:

static void imageFromCMD(){
     ProcessBuilder builder = new ProcessBuilder(
                "cmd.exe", "d:", "cd D:\\Java Projects\\imageProject",
                "screenshot-cmd");
     Process p = builder.start(); 
}

that code doesn't fail but i'm not getting the output (image in the dir) that i expect

I guess I'm missing the "sending" part, but how exactly can I do it?

3 Answers3

2

Can you try this?

ProcessBuilder processBuilder = new ProcessBuilder();
Path workingDir = Paths.get("D:\\Java Projects\\imageProject");
processBuilder.directory(workingDir.toFile()); // Edited here
processBuilder.command(".\\screenshot-cmd");
try {
    processBuilder.start();
} catch (Exception ex) {
    ex.printStackTrace();
}

An alternative option is to give the full path to the executable like so when creating a ProcessBuilder

ProcessBuilder processBuilder = new ProcessBuilder("D:\\Java Projects\\imageProject\\screenshot-cmd");
try {
    processBuilder.start();
} catch (Exception ex) {
    ex.printStackTrace();
}

One thing to note is that if you don't set the working directory when creating a ProcessBuilder, the directory of your main process is the working directory by default (basically from where your main class is being invoked), maybe try looking in there to see if the screenshots are being saved to that location

Trash Can
  • 6,608
  • 5
  • 24
  • 38
  • now it says "cannot run screenshot-cmd in the directory", but it works fine when i'm doing it manually and I made sure its the right name – junior_developer181 Jan 10 '20 at 01:23
  • What kind of exception did you get? `SecurityException` ? – Trash Can Jan 10 '20 at 01:31
  • java.io.IOException: Cannot run program "screenshot-cmd" (in directory "D:\Java Projects\imageProject"): CreateProcess error=2, The system cannot find the file specified – junior_developer181 Jan 10 '20 at 01:32
  • That's super interesting, can you try this `processBuilder.command(".\\screenshot-cmd");`, basically saying that that command should be found in the current working directory – Trash Can Jan 10 '20 at 01:37
  • Please try my latest edit attempt, if that doesn't fix it either, I really have no idea why. Also, does the executable have `.exe` extension? If so, you must include the extension as well in the `ProcessBuilder` configurations – Trash Can Jan 10 '20 at 01:47
  • ok I didn't get any error for this one, but nothing happen. the command suppose to create a `.png` file but it didn't – junior_developer181 Jan 10 '20 at 01:52
  • Maybe it didn't save the screenshot to the current directory? If you don't set the working directory when creating a `ProcessBuilder` the directory of your main process is the working directory by default (basically from where your main class is being invoked), maybe try looking in there – Trash Can Jan 10 '20 at 01:57
1

Here is my program to check the Java version. Hope this help.

import java.io.*; 
public class RunCMDByJava {
    public static void main(String[] args) throws Exception {
        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "java -version");
        pb.redirectErrorStream(true);
        Process p = pb.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = br.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
    }
}
Luan Pham
  • 76
  • 4
0

First find out the exact cmd.exe line to run and then stuff it into a ProcessBuilder, like this

new ProcessBuilder("cmd.exe", "/c", "cd /tmp & dir")

Please note that all commands should be passed to cmd.exe as one single argument.

speedogoo
  • 2,828
  • 2
  • 17
  • 19