Java run cmd security
NickName:user2130951 Ask DateTime:2018-02-26T18:59:16

Java run cmd security

I am trying to run an example to start the CMD. I am getting an error from the execution. The file is of course in the right place. So my guess is that this is a permission error. Can someone lead me on the right track on how to fix this problem?

I am running the program inside eclipse. But the same problem exists when trying to run the jar file.

Exception in thread "main" java.io.IOException: Cannot run program "c:\Windows\system32\cmd.exe /c dir": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at com.test.ProcessBuilderExample.main(ProcessBuilderExample.java:13)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified

package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;

public class ProcessBuilderExample {


    public static void main(String[] args) throws InterruptedException,
            IOException {

        ProcessBuilder pb = new ProcessBuilder("c:\\Windows\\system32\\cmd.exe /c dir", "This is ProcessBuilder Example from JCG");
        System.out.println("Run echo command");
        Process process = pb.start();
        int errCode = process.waitFor();
        System.out.println("Echo command executed, any errors? " + (errCode == 0 ? "No" : "Yes"));
        System.out.println("Echo Output:\n" + output(process.getInputStream()));   
    }
    private static String output(InputStream inputStream) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + System.getProperty("line.separator"));
            }
        } finally {
            br.close();
        }
        return sb.toString();
    }
}

Copyright Notice:Content Author:「user2130951」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48986899/java-run-cmd-security

More about “Java run cmd security” related questions

Java run cmd security

I am trying to run an example to start the CMD. I am getting an error from the execution. The file is of course in the right place. So my guess is that this is a permission error. Can someone lead ...

Show Detail

How to run Java from CMD, with Apache Commons libraries

I've made a program which uses the Apache Commons io and lang3 libraries. It runs fine in eclipse but I can't get it to run from cmd and it comes up with the following error: Exception in thread ...

Show Detail

java run cmd commands

I have some problem about run windows cmd ,when I run my code on GEL or some java tool,it works,but it run error that windows can't find "pscp".However,I export execute jar from eclipse and java -j...

Show Detail

How to open cmd in java

could you help me please? I am trying to open Command Prompt (CMD) from my Java code, but it gives me an exception. I tried to open other programs like notepad, but it works fine.I see other java

Show Detail

Run multiple cmd commands from java

I want to run multiple cmd commands succesively from java code one after the other. I want to use this one command line application which creates ssh connection and I want to run multiple commands

Show Detail

Able to compile but unable to run Java from cmd with jar files

I have a java file which uses jfreechart libraries, uses a text file from local drive and displays graph. Runs fine with eclipse. However, I want to run this from cmd prompt, other simple Java file...

Show Detail

How to run Java from cmd?

I have a multithreaded program that imports JSON that I want to run through CMD but right now, I can't because it says its not imported. How can I fix this problem so that I can always run the prog...

Show Detail

How to run java program from cmd line with classpath and native libraries?

Ok I give up - I dont have a lot of xp running java from cmd line and I've been at this for a while, tried endless combinations and I am just frustrated so here we go. I need to run a simple java ...

Show Detail

Run cmd commands through java processBuilder

I am trying to use ProcessBuild to run the cmd statement. ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start"); Process p = pb.start(); However, I can only open the cmd.exe I do no

Show Detail

Run cmd commands through Java

I found several code snippets for running cmd commands through a Java class, but I wasn't able to understand it. This is code for opening the cmd public void excCommand(String new_dir){ Runt...

Show Detail