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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:08:24+00:00 2026-06-13T00:08:24+00:00

I have a impl class which has several methods, they all basically call the

  • 0

I have a impl class which has several methods, they all basically call the script using runtime.exec with different argument list e.g.

public String doExport(String ruleIds, String fileName) throws Exception{
        StringBuffer cmd = new StringBuffer();
        cmd.append(SOME_SCRIPT + " -a export ");
        cmd.append(" -f " );
        cmd.append(fileName);
        cmd.append(" -r " );
        cmd.append(ruleIds);
        cmd.append(" 2>/dev/null");
        return execCmd(cmd.toString());
    }

    public String doImport(String fileName, String user, String iface) throws Exception {
        StringBuffer cmd = new StringBuffer();
        cmd.append(SOME_SCRIPT + " -a import ");
        cmd.append(" -f " );
        cmd.append(fileName);
        cmd.append(" -m " );
        cmd.append("user");
        cmd.append(" -u " );
        cmd.append(user);
        cmd.append(" -I " );
        cmd.append(iface);
        return execCmd(cmd.toString());
    } 
public String setRulesMode(String mode) throws Exception {
        String cmd = SOME_SCRIPT + " -a ";
        return execCmd(cmd.toString());
    }

Is there better way to do this? like encapsulate commands and arguments or more generic way of doing it? I tried to use enum but found it enum is better used when there are static/constant parameter list, so looking for better alaternative.

  • 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-13T00:08:25+00:00Added an answer on June 13, 2026 at 12:08 am

    I ‘d create “CommandBuilder” that wraps ProcessBuilder and is able to compose and run command line.

    Please pay attention that your attempt to redirect STDOUt to file will not work. Redirection is a shell feature. You can either run your command via shell (e.g. /bin/sh YOUR COMMAND >YOURFILE on unix or cmd -c YOUR COMMAND >YOURFILE on windows.

    Better way is to use Process.setOutputStream() from java. It is portable, easier to debug and maintain. This can be functionality of your CommandBuilder.

    EDIT

    Here are some tips to the CommandBuilder structure.

    public class CommandBuilder {
        public void setPrefix(); // e.g. cmd or /bin/sh But some kind of automatic logic should be implemented too, i.e. cmd for windows, /bin/sh for unix
        public void setCommand(); // for example ping
        public void addArgument(String value);  // e.g. myhost
        public void setInputStream(InputStream in);
        public void setOutputStream(OutputStream in);
    
        // add appropriate getters.
    
        public Process exec();
        public int execAndWait(); // returns process status
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object of class which has private constructor: class CL_GUIComponent { //
I have a small class, let's call uomvalue, which contains a UOM and a
I'm using eclipse. In the WEB-INF/lib folder I have the following jars. jstl-api-1.2.jar jstl-impl-1.2.jar
have written this little class, which generates a UUID every time an object of
I have a number of class, all with exactly the same interface. This interface
I have a common class say for eg Class A which extends AsyncTask and
We have: @Transactional(propagation = Propagation.REQUIRED) public class MyClass implementes MyInterface { ... MyInterface has
I have a table grid, which displays all users contained in the database. A
I have a Spring framework based Java web application, which has been built in
I have a RESTful service which injects an EJB(3.0) using the @EJB annotation. This

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.