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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:01:56+00:00 2026-06-11T09:01:56+00:00

I am making a program that will have the ability to run the java

  • 0

I am making a program that will have the ability to run the java compiler and jvm right from within it (Don’t ask me why I am reinventing the wheel, if your reply does not help, save it, I am already quite frustrated spending hours on solutions that do not work!). So far I have managed it to track whenever I input something in my textField that starts with java so that it will actually wrap up the text and give it a run like so:

    if(String.valueOf(object).startsWith("java")){
        try{
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(String.valueOf(object));
        }
        catch(Exception e){gsc.mainWindow.printf("error");}

Consider gsc.mainWindow.printf(...); my output to a JTextArea within a JFrame.

What I have managed now is to run the commands, but anything fails I shall be able to print it directly to my output. I know this has been answered a ton of times before, read about 10 ways to do this, but none of them worked or was understandable to the point that I could run it. I need the code to be simple enough as this will have to be outputting what the proccess will be writing in the default system’s Console (cmd,terminal) and then stop (I thought that this can be a method call alltogether). I am quite bad with this kind of stuff, even a multithread solution could fit my needs, nothing too professional really, I just need it to work. Any information you need, ask away!
Thanks 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-11T09:01:57+00:00Added an answer on June 11, 2026 at 9:01 am

    I don’t know you if you want to read this, but you know, in the Java world, you should always look for a solution before implementing your own. And the solution for common problems, most of the time, comes from Apache Commons or other Apache projects. Saying that everything but your solution doesn’t work or is too complicated to you will only cost you time and money (and your job, eventually).

    Apache Commons Exec is what you need to solve your problem faster and easier.

    —- Edit —-

    Here is some code of how to capture the output of the child process. There’s a class just for it, the PumpStreamHandler:

    DefaultExecutor exec = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler();
    exec.setStreamHandler(streamHandler);
    
    CommandLine commandline = CommandLine.parse(command);  //where command is your command line
    exec.execute(commandline);
    

    —- Edit 2 —-

    Here is the copy-paste solution you want to capture the message using an OutputStream:

    public abstract class LogOutputStream extends OutputStream {
    
    protected static final String LINE_SEPERATOR = System.getProperty("line.separator");
    public static final int DEFAULT_BUFFER_LENGTH = 2048;
    
    protected boolean hasBeenClosed = false;
    protected byte[] buf;
    protected int count;
    private int bufLength;
    
    public LogOutputStream() {
        bufLength = DEFAULT_BUFFER_LENGTH;
        buf = new byte[DEFAULT_BUFFER_LENGTH];
        count = 0;
    }
    
    public void close() {
        flush();
        hasBeenClosed = true;
    }
    
    public void write(final int b) throws IOException {
        if (hasBeenClosed) {
            throw new IOException("The stream has been closed.");
        }
        if (b == 0) {
            return;
        }
        if (count == bufLength) {
            final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH;
            final byte[] newBuf = new byte[newBufLength];
    
            System.arraycopy(buf, 0, newBuf, 0, bufLength);
    
            buf = newBuf;
            bufLength = newBufLength;
        }
        buf[count] = (byte) b;
        count++;
    }
    
    public void flush() {
        if (count == 0) {
            return;
        }
        if (count == LINE_SEPERATOR.length()) {
            if (((char) buf[0]) == LINE_SEPERATOR.charAt(0)
                    && ((count == 1) ||
                    ((count == 2) && ((char) buf[1]) == LINE_SEPERATOR.charAt(1)))) {
                reset();
                return;
            }
        }
        final byte[] theBytes = new byte[count];
        System.arraycopy(buf, 0, theBytes, 0, count);
        log(new String(theBytes));
        reset();
    }
    
    
    private void reset() {
        count = 0;
    }
    
    public abstract void log(String message);
    }
    

    Then just create a subclass of it, implement the public void log(String message) with the code that updates the UI, and it’s done.

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

Sidebar

Related Questions

I have been making a program that creates multiple CSV's from another source CSV
I have 2 questions. So i am making a python program that will backup
I am making a program that takes input from files that I have called
I'm making a program that will have a widget that has to be fixed
I am currently making a program that will send an email if the date
I am thinking about making a program that will need to send input and
I am trying to learn python and am making a program that will output
I am looking into making a c# program that will read in the logcat
Ok, I'm kinda new to java. I'm making a program that solves for one
I am making a application that the user will have to interact with one

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.