1

I want to get the result of windows command prompt function from Java.

Java code:

Process process2 = Runtime.getRuntime().exec("cmd /c getmac");

Are there any alternative libraries available other than Runtime?

Hemang
  • 390
  • 3
  • 20
  • Consider Apache Commons Exec as @v.ladynev suggested. And check this SO post: http://stackoverflow.com/questions/6295866/how-can-i-capture-the-output-of-a-command-as-a-string-with-commons-exec –  Dec 31 '15 at 08:09
  • @Aurelien , I like your comment. post it as an answer – rjdkolb Dec 31 '15 at 08:12
  • @VINOTH KUMAR , is your question about general executing commands or getting the mac address specifically ? Aurelien suggested this http://www.mkyong.com/java/how-to-get-mac-address-in-java/ which is much cleaner and not platform specific , but I think he removed his comment. – rjdkolb Dec 31 '15 at 08:24
  • Use a ProcessBuilder and redirect the error stream through the input stream and then read the Process's InputStream, something similar to [this](http://stackoverflow.com/questions/34537130/runtime-getruntime-execcommand-always-return-1/34537482#34537482) – MadProgrammer Dec 31 '15 at 08:33
  • @MadProgrammer It works fine. I'm working on a Java program to create a file which get the user name of the system and store it to the text file. when I make it into executable using Inno setup in Netbeans. It creates the text file in the installation folder. But I need to be created where the executable exists. In case pendrive, when my application executes it should create file in the pendrive not on the installation directory. How to overcome this? – Vinothkumar Dec 31 '15 at 11:53
  • @VINOTHKUMAR Set the working directory of the `ProcesBuilder` – MadProgrammer Dec 31 '15 at 12:19

1 Answers1

2

Use Apache Commons Exec. It can be used to get a console output of the running process.

A part of code from a real project

    private final Executor executor = new DefaultExecutor();

    private final ExecuteWatchdog watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);

    private final ProcessDestroyer shutdownHookProcessDestroyer = new ShutdownHookProcessDestroyer();

    private final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler() {
        @Override
        public void onProcessFailed(ExecuteException ex) {
            super.onProcessFailed(ex);
            LOG.error("Error executing xxx.exe", ex);
        }
    };

    public void startInSaveRecordMode(Long callId, File pathToResult) throws IOException {
        CommandLine commandLine = createModeUserPasswordCommandLine(SAVE_RECORD_MODE_COMMAND)
                .addArgument(ValidationUtil.toString(callId)).addArgument(
                        ValidationUtil.toString(pathToResult));
        execute(commandLine);
    }

    private void execute(CommandLine commandLine) throws IOException {
        Assert.notNull(pathToProcess);
        executor.setWatchdog(watchDog);
        executor.setProcessDestroyer(shutdownHookProcessDestroyer);
        executor.setStreamHandler(createStreamHandler());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing " + commandLine);
        }
        executor.execute(commandLine, resultHandler);
    }

    private CommandLine createModeUserPasswordCommandLine(String mode) {
        Assert.hasLength(sensormUser);
        Assert.notNull(sensormPassword);
        return createCommandLine().addArgument(mode).addArgument(sensormUser);
    }

private CommandLine createCommandLine() {
    return new CommandLine(pathToProcess);
}

private ExecuteStreamHandler createStreamHandler() {
    OutputEventsHandler eventsHandler = new OutputEventsHandler(eventsQueue);
    SensormLogHandler errorLogHandler = new SensormLogHandler(LOG, Level.ERROR);
    return new PumpStreamHandler(eventsHandler, errorLogHandler);
}

public int waitFor() throws InterruptedException {
    resultHandler.waitFor();
    return resultHandler.getExitValue();
}
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • thanks. It works fine. I'm working on a Java program to create a file which get the user name of the system and store it to the text file. when I make it into executable using Inno setup in Netbeans. It creates the text file in the installation folder. But I need to be created where the executable exists. In case pendrive, when my application executes it should create file in the pendrive not on the installation directory. How to overcome this? – Vinothkumar Dec 31 '15 at 11:25
  • @VINOTHKUMAR You are welcome. Sorry, I can't help you with this problem. – v.ladynev Dec 31 '15 at 11:38