Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 813149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:18:49+00:00 2026-05-15T01:18:49+00:00

I am trying to execute a program from the Java code. Here is my

  • 0

I am trying to execute a program from the Java code. Here is my code:

public static void main(String argv[]) {
    try {
      String line;
      Process p = Runtime.getRuntime().exec(
          "/bin/bash -c ls > OutputFileNames.txt");
      BufferedReader input = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
        System.out.println(line);
      }
      input.close();
    } catch (Exception err) {
      err.printStackTrace();
    }
}

My OS is Mac OS X 10.6.

If I remove the "> OutputFileNames.txt" from the getRuntime().exec() method, all the file names are printed on the console just fine. But I need it to be printed to a file.

Also, if I change the command to:

Process p = Runtime.getRuntime().exec(
    "cmd \c dir > OutputFileNames.txt"); 

and run it on Windows, it runs and prints the results in the file perfectly fine too.

I have read the other posts for executing another application from Java but none seemed to relate to my problem.

I would really appreciate any help I can get.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:18:50+00:00Added an answer on May 15, 2026 at 1:18 am

    To get the redirection to work as written, you need to do this:

    Process p = Runtime.getRuntime().exec(
          new String[]{"/bin/bash", "-c", "ls > OutputFileNames.txt"});
    

    The problem you were running into is the simple-minded way that that Runtime.exec(String) splits a command line into arguments.

    If you were to run that command (as is) at a shell prompt, you would have had to have entered it as:

    $ /bin/bash -c "ls > OutputFileNames.txt"
    

    because the “-c” option for “bash” requires the command line for the spawned shell as a single shell argument. But if you were to put the naked quotes into the Java String, the Runtime.exec(String) method still get the splitting wrong. The only solution is to provide the command arguments as an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would make sure your MySQL database has proper indexes… May 16, 2026 at 4:40 pm
  • Editorial Team
    Editorial Team added an answer the answer is a vague "yes", by serializing its properties/settings… May 16, 2026 at 4:40 pm
  • Editorial Team
    Editorial Team added an answer There is no specific reason that they should be static.… May 16, 2026 at 4:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have to execute my program with this line: java -DImport=data.txt -Din=input.dat -Dout=output1.dat main.Main
I'm trying to send keystrokes from my C# program to a Java application The
Hello everyone I'm trying to improve my Java skills by solving some problems from
Newbie to C++ learning by converting a java program to c++. The following code
I am for a long time trying to get a picture how is program
I'm trying to run an executable using Lua's os.execute() function. If I do something
I am trying to make my program ignore Ctrl + C in unix which
I'm trying to execute a batch file to move a bunch of files around
I am trying to execute a function every time a field changes. Just to
I am trying to get my feet wet with JNI because I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.