Bohdan Kyryliuk
Bohdan Kyryliuk

Reputation: 11

Printing commands to bat/cmd file with Java

How to print commands to bat/cmd file using Java? I have created a method that opens this bat file and now the program should write commands to this bat file. For instance, I have a string variable "Command" and the program must write this command to bat file.

Here I attach the code.

private static void openBat(){
    File file = new File(lockerPath);
    try {
        if (file.exists()) {
            Process pro = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + lockerPath);
            pro.waitFor();
        } else {
            System.out.println("file does not exist");
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

This is the code to open bat file, and the next code is to write commands:

private static void printing(int password ){
    try {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/C", lockerPath);
        Process process = processBuilder.start();
        process.waitFor();
        List<String> commands = new ArrayList<>();
        commands.add(String.valueOf(password));
        processBuilder.command(commands);
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

It doesn't write anything to the file.
I will be very grateful for your help.

Upvotes: 1

Views: 375

Answers (1)

Abra
Abra

Reputation: 20914

I still don't think I completely understand your question. If you just want to simulate the user entering a value to a batch file via a java program, then the below code does that.

First I wrote a batch file.

@echo off
set /P pw=
echo You entered: %pw%

It simply waits for the user to enter a value and assigns that value to a variable named pw. After the user enters the value, the batch file displays the entered value.

Here is the java code that runs the above batch file and enters a value.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "getusrpw.bat");
        try {
            Process p = pb.start(); // throws java.io.IOException
            BufferedReader stdout = p.inputReader();
            BufferedReader stderr = p.errorReader();
            BufferedWriter stdin = p.outputWriter();
            stdin.write("secret");
            stdin.newLine();
            stdin.flush();
            String output = stdout.readLine();
            while (output != null) {
                System.out.println("OUT> " + output);
                output = stdout.readLine();
            }
            String error = stderr.readLine();
            while (error != null) {
                System.out.println("ERR> " + error);
                error = stderr.readLine();
            }
            int exitStatus = p.waitFor(); // throws java.lang.InterruptedException
            System.out.println("Process exit status = " + exitStatus);
        }
        catch (InterruptedException | IOException x) {
            x.printStackTrace();
        }
    }
}
  • The name of the batch file is getusrpw.bat.
  • stdout is for reading output generated by the batch file.
  • stderr is for reading error output. Note that there may be no error output.
  • stdin is for sending input to the batch file.
  • Note that methods inputReader, outputWriter and errorReader were added in JDK 17. If you are using an earlier version, use methods getInputStream, getOutputStream and getErrorStream, respectively.
  • The Java program enters the value secret.
  • The Java program prints the output generated by the batch file.

When I run the above Java code, it produces the following output:

OUT> You entered: secret
Process exit status = 0

Upvotes: 1

Related Questions