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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:20:03+00:00 2026-05-27T14:20:03+00:00

I am working on a Java Project for my own learning, what i have

  • 0

I am working on a Java Project for my own learning, what i have made is a class which can both read and write to external process using Runtime.getRuntime().exec(cmd);

Now i was wondering is there any special way of checking if a particular software/tool is installed on the system.

Like i use sshpass utility to remotely login to other machines, and if it is not there already i would like to install it using my program. But for this how should i go about checking if it exists there or not?

The idea i have in my mind is to run the command and see the response, if the returned string matches particular expression based on that i would decide it’s existence or non-existence.

Do you think it is the right approach or is there any other way to find out this?

Like on windows, i think there are cmdline utilities like ftype, assoc etc,
thank you,

  • 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-27T14:20:04+00:00Added an answer on May 27, 2026 at 2:20 pm

    If you already know the name of the software binary (which is usually the same to process name) you can use which command.

    You can test it in bash/shell
    which firefox
    /usr/bin/firefox

    Also I can supply you an example written in C# of bash output reading:

    string output = string.Empty;

    string output = string.Empty;
    
    try
    {
        // Sets up our process, the first argument is the command
        // and the second holds the arguments passed to the command
        ProcessStartInfo ps = new ProcessStartInfo("bash");
        ps.Arguments = "-c 'firefox'";
        ps.UseShellExecute = false;
    
        // Redirects the standard output so it reads internally in out program
        ps.RedirectStandardOutput = true;
    
        // Starts the process
        using (Process p = Process.Start(ps))
        {
            // Reads the output to a string
            output = p.StandardOutput.ReadToEnd();
    
            // Waits for the process to exit must come *after* StandardOutput is "empty"
            // so that we don't deadlock because the intermediate kernel pipe is full.
            p.WaitForExit();
        }
    }
    catch
    {
        // TODO manage errors
    }
    

    If the bash output is multi-line you can pre-filter it by piping to the grep command:

    ps.Arguments = "-c 'cpuid | grep MySearchTerm'";
    

    EDIT 1: Reply to comments

    The major problem is the software installation, which requires “administrative” rights.
    I’ve tried to create a workaround, but the following line breaks all code:

    process = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","'echo RIadminXsrv1 | sudo -S apt-get install telnet -qy'"});
    

    While in terminal the following command will actually attempt to install telnet (you might have to insert your user into /etc/sudoers to reproduce it on your PC).

    /bin/echo myUserPass | /usr/bin/sudo -S /usr/bin/apt-get install telnet -qy

    In java it will simply print (echo output) the remaining part of the command:

    myUserPass | /usr/bin/sudo -S /usr/bin/apt-get install telnet -qy

    This happens because we are simply executing /bin/echo command with a lot of parameters.
    I thought that it is possible to actually run the entire set of commands using bash:

    bash -c '/bin/echo myUserPass | /usr/bin/sudo -S /usr/bin/apt-get install telnet -qy'
    

    ..but it’s not, because bash -c '..' in Java doesn’t work like it should. It says that -c ‘echo …’ script file can not be found, so I suppose that it misinterprets -c option.
    BTW I have never had this kind of problem in Mono C#.

    Here is the entire snippet:

    package javaapplication1;
    
    import java.io.*;
    
    public class JavaApplication1 {
    
        public static void main(String[] args) {
    
            Process process;
            String softwareToCheck = "telnet"; // Change here
    
            try
            {       
                if(!_softwareExists(softwareToCheck))
                {
                    System.out.println("Installing missing software..");
                    process = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","'echo RIadminXsrv1 | sudo -S apt-get install telnet -qy'"});
    
                    try
                    {
                        process.waitFor();
                    }
                    catch(InterruptedException e)
                    {
                        System.out.println(e.getMessage());
                    }
    
                    if(!_softwareExists(softwareToCheck))
                    {
                        System.out.println("Software is still missing!");
                    }
    
                }
                else
                {
                    System.out.println("Software is installed!");
                }
            }
            catch(IOException e)
            {
                System.out.println(e.getMessage());
            }        
        }
    
        private static boolean _softwareExists(String binaryName) throws IOException
        {
            String line;
            ProcessBuilder builder;
            BufferedReader reader;
            Process process;
    
            builder = new ProcessBuilder("/usr/bin/which", binaryName);
            builder.redirectErrorStream(true);
            process = builder.start();
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            try
            {
                process.waitFor();
            }
            catch(InterruptedException e) {
                System.out.println(e.getMessage());
            }
    
            while ((line = reader.readLine ()) != null)
            {
                break; // Reads only the first line
            }
    
            return (line != null && !line.isEmpty());
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a Java project for a class for a while
I have a java project that I'm working on which was working until a
Im working on a java project for a class which has a user enter
I'm working on a java project which depends on both 'javax.lang' and 'javax.annotation'. I
I'm working on a Java project in which I have the getter method below
I am working on a JAVA project in which there are multiple terminals. These
I am working on a Java project and need to have a keypress simulate
I have created a couple of java working sets for a project in my
I'm working on a java project full of Hibernate (3.3.1) mapping files that have
I'm working with a Java sort-of-application-server (Smartfox) which can run multiple applications (extensions) but

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.