• 0

[Java] Print output of shell command


Question

So Im working on a little project right now and currently im trying to run a shell command under java.

Everything works great, except I cant print the output of the shell command until its completely done executing. For this example / testing im using the rsync command in bash. How can I get java to print out whats happening in shell?

This code is running in its own method and in another thread so the ActionListener I add to the button that'll execute it wont hold it up. Its working like I expect except for it not printing right away. A lot of stuff online says to redraw / update, but i've done both to my Text Area and nothing.

Any ideas guys?

Code:


// Trial Code

String shellcommand = new String("");
shellcommand = "time rsync -Phav /Users/sikh/Downloads/ /Users/sikh/Desktop/Test";

Runtime run = Runtime.getRuntime();
Process cmdProcess = null;

try {
cmdProcess = run.exec(shellcommand);
} catch (IOException e) {
e.printStackTrace();
}

try {
cmdProcess.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}

BufferedReader reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
String line = "";
try {
while ((line = reader.readLine()) != null)
{
mainclass.setText(line);
} // end while
} catch (IOException e) {
e.printStackTrace();
} // end try

// End Trial Code
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1134404-java-print-output-of-shell-command/
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • In addition to the political nature of Elon musk aside. AI Chat bots still have issues that is still a bit of a black box they are still trying to figure and are still not trust worthy to do what they say at this time. This is an area of “go fast and break things” mantra with tech is a very bad idea.
    • Electron sucks. I was much happier when Teams moved off it.
    • iOS 27 will focus on features for Apple's first foldable phone coming 2026 by Taras Buria Foldable smartphones are hardly a novel thing anymore. The form factor has been around for a while, and Samsung, the pioneer of the idea, is already at the seventh generation. Now, with the concept and tech being plenty mature, Apple is joining the party with its first foldable, rumored to arrive in 2026. According to Mark Gurman from Bloomberg, Apple's next iOS 27 update will prioritize new features for the rumored foldable iPhone. The development of iOS 27 is starting soon, and Apple wants to put a lot of focus on the folding experience. With the first foldable iPhone, Apple is entering a well-developed market, and the company is not expected to bring major innovations, unlike the original Apple Vision Pro. Apple is reportedly focusing on a design similar to the Galaxy Z Fold7 with an inward-folding display. Still, the first iPhone is rumored to feature some notable improvements for the inner display crease and the hinge. Foldable phones are becoming increasingly popular in China, and Apple can no longer ignore the market. Offering an expensive foldable iPhone will help the company boost revenue and reignite interest in its main product. There are plenty of users who are interested in giving a book-like foldable a try, but having Android as the only platform choice is a big turn-off for many. With Apple joining the party, the form factor will become much more interesting to many users. Still, those users will have to make sure their pockets are plenty deep. The first foldable iPhone is not expected to cost less than $2,000, so the sticker shock of the original iPhone X from 2017 and Apple Vision Pro from 2023 is coming next year. Source: Bloomberg
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      Michael Scrip
      205
    3. 3
      ATLien_0
      201
    4. 4
      Xenon
      138
    5. 5
      +FloatingFatMan
      117
  • Tell a friend

    Love Neowin? Tell a friend!