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?

7

3 Answers 3

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

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

7 Comments

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
What kind of exception did you get? SecurityException ?
java.io.IOException: Cannot run program "screenshot-cmd" (in directory "D:\Java Projects\imageProject"): CreateProcess error=2, The system cannot find the file specified
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
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
|
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);
        }
    }
}

Comments

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.

3 Comments

What happens if the pc is Mac? meaning does not have cmd.exe ?
Since the question is on CMD, my understanding is that this must be a Windows.
Oh, yea, my bad.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.