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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:13:31+00:00 2026-06-02T12:13:31+00:00

For my server coded in java I want to add a console. I connect

  • 0

For my server coded in java I want to add a console. I connect to my server using a socket.
Here is the code I’ve made for the console:

On my server:

public class ServerConsole
{   
    public String exec(String[] cmd)
    {
        try
        {
            Process child = Runtime.getRuntime().exec(cmd);

            InputStream in = child.getInputStream();
            StringBuffer buffer = new StringBuffer();
            int c;
            while ((c = in.read()) != -1)
            {
                buffer.append((char)c);
            }
            in.close();

            return buffer.toString();
        }
        catch (Exception e) {}

        return "FAILED";
    }
}

This class execute the given command and returns a string that contains the content of the console after execution.

I call this method like that:

String cmd_data_cmd = inputStream.readUTF();
String[] dataCmd = cmd_data_cmd.split("#");
OSCmd osCmd = new OSCmd();
outputStream.writeUTF(osCmd.exec(dataCmd));

Where inputStream is the stream I use with my socket. It works well!

Now, on the client side, I’ve made that:

String[] cmd = cmd_input.getText().split(" ");
String new_cmd = "";
for (String part : cmd)
    new_cmd += (new_cmd.equals("") ? "": "#") + part;

this.outputSocket.writeUTF(new_cmd);
DataInputStream result_input = new DataInputStream(this.input);
String tmp = result_input.readUTF();
System.out.println(tmp);

This should returns me the result displayed in the console but actually, nothing happens. It just freezes when I start that part of code.

Any idea how to do that?

Thanks.

  • 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-02T12:13:33+00:00Added an answer on June 2, 2026 at 12:13 pm

    Here is the solution:

    String[] cmd_exec = {};
    String os_name = System.getProperty("os.name").toLowerCase();
    if (os_name.indexOf("win") >= 0)
        cmd_exec = new String[]{"cmd.exe", "/c", cmd};
    else if (os_name.indexOf("mac") >= 0)
        cmd_exec = new String[]{"/usr/bin/open", "-a", cmd};
    else if (os_name.indexOf("nix") >= 0 || os_name.indexOf("nux") >= 0)
        cmd_exec = new String[]{"/bin/bash", cmd};
    else if (os_name.indexOf("sunos") >= 0)
        cmd_exec = new String[]{"/bin/bash", cmd};
    
    Process child = Runtime.getRuntime().exec(cmd_exec);
    
    String line;
    while ((line = stdInput.readLine()) != null)
    {
        buffer.append("\t" + new String(line.getBytes("UTF-8"), "UTF-8") + "\n");
    }
    stdInput.close();
    while ((line = stdError.readLine()) != null)
    {
        buffer.append("\t" + new String(line.getBytes("UTF-8"), "UTF-8") + "\n");
    }
    stdError.close();
    
    child.destroy();
    

    Hope this will help someone else.

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

Sidebar

Related Questions

Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer
I make a java socket server and c socket client, here are the code
I want to write a server side code in Java to access DB. A
Does anyone have any code for connecting to a remote server using java and
I am having difficulty connecting my java code to sql server database. I can
Getting these errors in some java code http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html Exception in thread Thread-0 java.lang.RuntimeException: Cannot
I am writing some java TCP/IP networking code ( client - server ) in
I'm trying to make a multithreaded server/client app with java ! this code is
I have a server coded in C. After the accept() , I want to
I am owning some websites, in php, java and html coded. I want them

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.