Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ubuntu Java: How to run Linux shell scipt from Java program and fetch back its output

Posted on 2009-07-14
8
Medium Priority
?
540 Views
Last Modified: 2012-05-07
Hi: I am using Java (HTTPUnit) in Linux machine. I am using HTTPUnit multi thread to visit a web application (Hibernate, Spring, tomcat, mysql). Experts in this forum give me the script to see Tomcat JVM memory consumption. Now when I use HTTPUnit to visit a page, then I want to check memory usage by the program themself. And see how much memory consumption it takes. If the memory consumption is bigger, I could write it to some file. My question is from Java program how to run a linux script and how to get output back.

The script is as follows: It could be stored in some executable file called checkTomcatJVM.sh.

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | grep java
0
Comment
Question by:JianJunShen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 24848897
you can use the Runtime class from java to get the total memory and freememory!

check Runtime.freeMemory()
Runtime.totalMemory
0
 
LVL 6

Expert Comment

by:jwenting
ID: 24848934
Use ProcessBuilder to execute the external process.
Wait for the process to complete, and capture its output stream.
0
 
LVL 14

Expert Comment

by:flob9
ID: 24848936
Runtime.getRuntime().exec(new String[] {"script","param"});
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:JianJunShen
ID: 24848983
Runtime.getRuntime().exec(new String[] {"script","param"});

This sounds a good solution to run script. But how to fetch output?
0
 
LVL 14

Accepted Solution

by:
flob9 earned 2000 total points
ID: 24849040
Something like this :

Process proc = Runtime.getRuntime().exec(new String[] {"script","param"});

byte[] buff = new byte[1024];

InputStream out = proc.getInputStream();
int nb = out.read(buff);


But, as mentionned before, calling external process for this purpose is quite dirty.
0
 

Author Comment

by:JianJunShen
ID: 24849192
Oh; sorry , I push the wrong button. Now the excellent answer would be closed. My mistake.
0
 

Author Closing Comment

by:JianJunShen
ID: 31603225
Excellent answer and very fast.
0
 
LVL 92

Expert Comment

by:objects
ID: 24854763
you need to read in a loop to ensure you get all output written to stdout


http://helpdesk.objects.com.au/java/runtime-exec-locks-up
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question