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 8661559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:31:31+00:00 2026-06-12T16:31:31+00:00

I want my Java program to run the command echo text > /home/maxbester/test.txt on

  • 0

I want my Java program to run the command echo "text" > /home/maxbester/test.txt on several Unix based systems.

My code looks like:

private static final Logger LOG = Logger.getLogger(MyClass.class);
public String run(String cmd)  {
    String res = null;
    InputStream is = null;
    try {
        final Runtime rt = Runtime.getRuntime();
        final Process p = rt.exec(cmd);
        int exitStatus = -1;
        try {
            exitStatus = p.waitFor();
        } catch (InterruptedException e) {
            L0G.error(e.getMessage(), e);
        }
        is = p.getInputStream();

        if (exitStatus == 0) {
            if (is != null && is.available() > 0) {
                StringWriter stringWriter = new StringWriter();
                IOUtils.copy(is, stringWriter);
                res = stringWriter.toString();
            } else {
                L0G.error("InputStream is not available!");
            }
        }
    } catch (SecurityException e) {
        L0G.error(e.getMessage(), e);
    } catch (IOException e) {
        L0G.error(e.getMessage(), e);
    }
    return res;
}

When cmd equals echo "text" > /home/maxbester/test.txt and the file test.txt exists, res contains "text" > /home/maxbester/test.txt (and not echo "text" > /home/maxbester/test.txt, the echo disapeared) and test.txt is empty. However the exit value is 0 (so it should have worked correctly).

I run manually echo "text" > /home/maxbester/test.txt. Nothing was returned and the exit value was also 0.

So what’s going wrong with the exec command?

  • 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-06-12T16:31:32+00:00Added an answer on June 12, 2026 at 4:31 pm

    If you want to write text to a file in Java, you should take a look at FileWriter instead of relying on shell-specific details such as stream redirection.

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

Sidebar

Related Questions

I want to facilate my client to run java program through UNIX command prompt
I want to run a a java program on a remote computer 24 x
I want run a script as follows: runner: ssh 'java program &' ssh 'java
I want to pass command line arguments to my java program that is invoked
I'm trying to get my java program to run an svn command from the
I want to run a java program on a linux server in profiling mode.
I run a Java program with the following command line (Edit: in NetBeans 6.8
Inside a Perl script, I want to run a Java program that takes in
Possible Duplicate: run shell command from java I want to export some variable so
I want to pass the string !changeme! to a java program on the command

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.