0

I want to run "ls" command in java and my code is- Note:- I am using WINDOWS.

import java.io.IOException;

public class Example
{
    public void fn()
    {
        Runtime run = Runtime.getRuntime();  
        Process p = null;  
        String cmd = "ls"; 

        try {  
            p = run.exec(cmd);  

            p.getErrorStream();  
            p.waitFor();
        } catch (IOException | InterruptedException e) {  
            e.printStackTrace();  
            System.out.println("ERROR.RUNNING.CMD");  
        } finally {
            p.destroy();
        }
    }

    public static void main(String[] args) throws IOException
    {
        Example sp = new Example();
        sp.fn();
    }
}

but I am getting following error while running this code in eclipse-

java.io.IOException: Cannot run program "ls": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at Example.fn(Example.java:12)
    at Example.main(Example.java:28)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 6 more
Exception in thread "main" ERROR.RUNNING.CMD
java.lang.NullPointerException
    at Example.fn(Example.java:23)
    at Example.main(Example.java:28)

what needs to be corrected? what library, etc. should I add to execute this piece of code?

6
  • 7
    I want to run "ls" command in java Are you sure? Because if what you need is a listing of files on the system, this is one terrible way to do it for multiple reasons Commented Mar 7, 2017 at 16:23
  • How are you running above program ? Commented Mar 7, 2017 at 16:24
  • try run.exec("bash", "-c", "ls"); Commented Mar 7, 2017 at 16:26
  • 2
    Silly question, but you are running on Linux and the ls command works on there? Commented Mar 7, 2017 at 16:27
  • Basically, your program seems to be correct - I was able to execute it on my system (Arch Linux) without errors. Commented Mar 7, 2017 at 16:36

1 Answer 1

4

If you want to run any command from Java ensure that executable of this file is present in PATH environment variable.

Or at least you have to set working directory to /usr/bin or similar.

One more solution is to use absolute path to executable, e.g.:

Runtime.getRuntime().exec("/usr/bin/ls");

But it is bad way to specify absolute path to any file.

Why don't you use File#listFiles()?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.