I Harris Marfel I Harris Marfel - 1 year ago 68
Java Question

Execute linux command in java and display output to html table

I have jsp code "df -h" for display disk information on the website.
How i can show the output to html with table?
Bellow the code:

String[] disk;
String line;
String process;
Process p;
BufferedReader input;
p = Runtime.getRuntime().exec("df -h");
input = new BufferedReader(new InputStreamReader(p.getInputStream()));
disk = input.readLine().split("\\s+");
<tr bgcolor="#f0f0f0">
<td colspan="2">
<td align="center">
<td align="center">
<td align="center">
<td align="center">
<td align="center">
while ((line = input.readLine()) != null) {
<td colspan="2"><% out.println(disk[0]); %></td>
<td align="center"><% out.println(disk[1]); %></td>
<td align="center"><% out.println(disk[2]); %></td>
<td align="center"><% out.println(disk[3]); %></td>
<td align="center"><% out.println(disk[4]); %></td>
<td align="center"><% out.println(disk[5]); %></td>

when on linux i execute "df -h" it showing like bellow:

[root@svr1 apache-tomcat-7.0.32]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_svr1-root 29G 5.3G 23G 19% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 17M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 497M 163M 334M 33% /boot
tmpfs 380M 0 380M 0% /run/user/0
[root@svr1 apache-tomcat-7.0.32]#

But at html i got like bellow:

how i can fix it? or what code that i need to change?

Thanks before.

Answer Source

You need to move this line inside the while loop:

disk = input.readLine().split("\\s+");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download