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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:01:28+00:00 2026-06-02T22:01:28+00:00

I am writing a program in Java that needs to use terminal command to

  • 0

I am writing a program in Java that needs to use terminal command to work.
My function basically looks like this :

public void sendLoginCommand() throws IOException
{
    System.out.println("\n------------Sending Login Command------------\n");
    String cmd="qskdjqhsdqsd";
    Runtime rt = Runtime.getRuntime();
    Process p=rt.exec(cmd);
}
public Process sendPassword(String password) throws IOException
{
    System.out.println("\n------------Sending Password------------\n");
    String cmd=password;
    Runtime rt = Runtime.getRuntime();
    Process p=rt.exec(cmd);
    return p;
}
public void login(String password) throws IOException
{
    sendLoginCommand();
    Process p = sendPassword(password);
    System.out.println("\n------------Reading Terminal Output------------\n");
    Reader in = new InputStreamReader(p.getInputStream());

    in = new BufferedReader(in);
    char[] buffer = new char[20];
    int len = in.read(buffer);
    String s = new String(buffer, 0, len);
    System.out.println(s);
    if(s.equals("Password invalid.")) loggedIn=false;
    else loggedIn=true;
}

Here, the program sends correctly th p4 login command, but then, the terminal asks for a password.
When I use the same lines that with the sendLoginCommand(), the program returns an error.
Apparently, we can send only standard commands throught Process.
I was hoping that someone knew how to send a normal string to the terminal

Thank you in advance

  • 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-02T22:01:30+00:00Added an answer on June 2, 2026 at 10:01 pm

    I have found the answer to my question.

    The problem was that the second response of the terminal was in fact in the first one, and the password had to be sent in the middle of it.
    Here is the code (I agree, my explanation is a little vague) :

        String s="";
        Process p = Runtime.getRuntime().exec("p4 login");     
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));    
        char a=(char)in.read();
        while(a>0 && a<256)
        {
    
            a=(char)in.read();
            if(nb==14) new PrintWriter(p.getOutputStream(),true).println(password); 
            if(nb>16) s=s+a;
            nb++;
        }
        if(s.startsWith("User")) loggedIn=true;
        else loggedIn=false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a JAVA program for work that at some point needs to
I am writing a program that needs to run a java.jar server. I need
I'm writing (under linux or windows+cygwin) a java program that needs to run a
I'm writing a program (in Java) that needs to extract links from webpages. I'm
I am writing a java program that needs a file open dialog. The file
So I am writing a program in Java that needs to be able to
I'm writing a Java program MyAwesomeProgram that uses Process' exec function to run bash
I'm writing a java program that goes out and searches twitter, finds certain instagram
I'm currently writing a java program that requires some Data to run. The data
I am writing an access control program in Java that captures passwords using JPasswordField

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.