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

  • SEARCH
  • Home
  • 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 8332899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:51:12+00:00 2026-06-09T02:51:12+00:00

I want to run some unix/shell commands in Java and process the output. I

  • 0

I want to run some unix/shell commands in Java and process the output. I used getRuntime.exec(), but it is not giving correct output for some commands like ls directory_path/*.tar.gz . For that command my program is not giving any output but it is giving error saying No such file or directory. But the same command is giving correct output on command prompt.

Is there any other way to execute commands which include wildcards in Java?

  • 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-09T02:51:14+00:00Added an answer on June 9, 2026 at 2:51 am

    Is there any other way to execute commands which include wildcards in Java?

    Yes there is.

    You have three potential problems in your code:

    1. Assuming you want to see the output of the external command you need to take care of the output it produces, and print it yourself.

      Follow the example I’ve provided over here:

      • How to make a java program to print both out.println() and err.println() statements?
    2. The shell expansion of * doesn’t work if you naively go through Runtime.exec.You have to go through /bin/bash explicitly.

    3. If you try to list the content of directory_path/*.tar.gz you should know that (if you’re not starting with a /) directory_path will be resolved relative to the current working directory. Try at first to give the absolute path to make sure it works, and then try to figure out why it can’t find the relative path.

    Here’s a complete example that should work:

    Process proc = new ProcessBuilder("/bin/bash", "-c",
                                      "ls /.../directory_path/*.tar.gz").start();
    
    Reader reader = new InputStreamReader(proc.getInputStream());
    int ch;
    while ((ch = reader.read()) != -1)
        System.out.print((char) ch);
    reader.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute some Unix commands from the java code. I want to
I want my Vim to run some commands, such as setlocal spell spelllang=en_us when
when I want to execute some shell script in Unix (and let's say that
I want to run some system commands (to fix things) before running an executable.
I want to execute some batch jobs from my unix box thro shell scripts.
I'm writing some Perl code. I want it to run on Windows and Linux/UNIX/OSX.
So if I want to run a binary using exec() on a child process
I want to facilate my client to run java program through UNIX command prompt
I need to run some commands within a Ruby file, but rather than running
I want to run some basic checks when my ASP.NET server goes up. Mostly

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.