Running Top command in Java
hi friends,
when i run Top command in Linux from java by using Runtime.getRuntime.exec(), it is giving the following error---
top: tcgetattr() failed : Invalid Argument
While this command is working properly from shell.
when i run Top command in Linux from java by using
Runtime.getRuntime.exec(), it is giving the following
error---
top: tcgetattr() failed : Invalid Argument
command is working properly from shell.top needs a terminal to run; it's a silly and ugly little program. When you
fire up a process using the exec() method you don't have a terminal
environment for the process. Try to run 'ps' instead.
kind regards,
Jos
Similar Messages
-
Help in running top comman in java
hi frineds i want to run top command in java and store tht input into a file....any pls give suggestions.i will be thank full to u.
bye
keerthi.Can you read? Why don't you read manual for top?
Start "top b n 1" and read output as easy as any other output. -
Executing the top command through Java in linux
I am trying to execute the top command in Java as
Runtime.getRuntime().exec("top -n 1 >a.log");
But the command is not working in linux only through java.When i run the same command through the prompt it is working fine.Also all other commands are working fine.Is there any issue with top in linux?flounder wrote:
Try reading [this article|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html]. If it does not answer your problem then try seaching for other similar articles.
The cited article does provide an answer to the problem since it explains how the shell meta character '>' must be interpretted by a shell. The code as presented does not invoke a shell. -
Running ls command from Java stroed procedure no output
Hi ,
I am trying to run ls command from java stored procedure in oracle
Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("output of the command run:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
from java stored procedure in oracle.
i get output of println statments but it does not go into while loop to print from stdInput.
Result of running Java stored procedure is -
output of the command run:
Call completed.
when i run the program on client side it works fine.
Has anybody tried this from java stroed procedure.
Thanks,
JagJag,
Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
SQL> /
output of the command run:
init.ora
initDBPart9i.DBPSun01.ora
initdw.ora
lkDBPART9I
orapw
orapwDBPart9i
spfileDBPart9i.ora
Done
PL/SQL procedure successfully completed.
But, I did need to change a line of your code to this:
Process p = Runtime.getRuntime().exec("/usr/bin/ls");
your original was:
Process p = Runtime.getRuntime().exec("ls");
You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
Hope this helps,
-Dan
http://www.compuware.com/products/devpartner/db/oracle_debug.htm
Debug PL/SQL and Java in the Oracle Database -
How to run DOS command in Java environment?
Can i run DOS command in Java environment?
I did like this:
Runtime r = Runtime.getRuntime();
r.exec("cmd.exe");
r.exec("set classpath=%CLASSPATH%;.\\tmp")
but failed.
However if I run the java command, it runs successfully.
r.exec("javac Test.java");
r.exec("java Test");
how should I do so that i can run the DOS commands metioned above in Java Environment?
thanks a lot.Have a look at http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
This may help. I wonder if this is ok ?
Runtime r= Runtime.getRuntime();
r.exec("cmd.exe /C set classpath=%CLASSPATH%;.\\tmp\""); -
I want run some command using java
hi all,
i want run command using java code.
from cell prompt when i run this command 'mysqldump test > /home/DBNAME.sql'
it will create DBNAME.sql file.
but i want to run this command using java code
i tried the following code but it did not work.
is any other way is their?
try {
Runtime.getRuntime().exec("setxkbmap nudi");
} catch(IOException ioe) {
ioe.printStackTrace();
thanks in advance
dayahello,
thanks for replay
i am sorry the above code should be like this.(the above code is working fine)
public class ExportTest{
public ExportTest(){
try {
Runtime.getRuntime().exec("mysqldump test > '/root/DBNAME.sql'");
} catch(IOException ioe) {
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
public static void main(String args[]){
new ExportTest();
}when run above class, it not creating DBNAME.sql file.
when run in command prompt it creating DBNAME.sql
($ mysqldump test > /root/DBNAME.sql)
but i want to run this command from java code, that's way tried to do using above
code, but it won't create DBNAME.sql
is it wrong what i am doing? or any other way?
thanks inadvace
daya -
Urgent!! running system command in java
i am trying to run system command(windows) in java. I am getting
exception as java.io.IOException: CreateProcess: c:\dir
Please Help.
Here is the code
import java.io.*;
public class RunCommand {
public static void main(String[] args) {
try
//run dir command
Process p = Runtime.getRuntime().exec("c:\\dir");
BufferedReader stdInput=new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError =new BufferedReader(new
InputStreamReader(p.getErrorStream()));
//read the output
System.out.println("here is the output:\n");
while((s=stdInput.readLine())!=null){
System.out.println(s);
System.out.println("here is the error if any");
while((s=stdError.readLine())!=null){
System.out.println(s);
System.exit(0);
}//end try
catch (IOException e)
System.out.println("exception Happened" );
e.printStackTrace();
System.exit(-1);It works fine if you replaceProcess p = Runtime.getRuntime().exec("c:\\dir");withProcess p = Runtime.getRuntime().exec("cmd /c dir");And don't forget to define your s variable.
-
Run shell commands using java program
Hi guys,
I am trying to run shell commands like cd /something and ./command with arguments as follows, but getting an exception that ./command not found.Instead of changing directory using "cd" command I am passing directory as an argument in rt,exec().
String []cmd={"./command","arg1", "arg2", "arg3"};
File file= new File("/path");
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd,null,file);
proc.waitFor();
System.out.println(proc.exitValue())
BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
catch(Exception e)
{e.printStackTrace();
So can anyone please tell me what is wrong with this approach? or is there any better way to do this?
Thanks,
Hardikwarnerja wrote:
What gives you the idea that the process to execute is called "./command"? If this is in Windows, it is "cmd.exe" for example.It does not have to be cmd.exe in Windows. Any executable or .bat file can be executed as long as one either specifies the full path or the executable is in a directory that is in the PATH.
On *nix the file has to have the executable bit set and one either specifies the full path or the executable must be in a directory that is in the PATH . If the executable is a script then if there is a hash-bang (#!) at the start of the first line then the rest of the line is taken as the interpreter to use. For example #!/bin/bash or #!/usr/bin/perl .
One both window and *nix one can exec() an interpreter directly and then pass the commands into the process stdin. The advantage of doing this is that one can change the environment in one line and it remains in effect for subsequent line. A simple example of this for bash on Linux is
import java.io.OutputStreamWriter;
import java.io.Writer;
public class ExecInputThroughStdin
public static void main(String args[]) throws Exception
final Process process = Runtime.getRuntime().exec("bash");
new Thread(new PipeInputStreamToOutputStreamRunnable(process.getErrorStream(), System.err)).start();
new Thread(new PipeInputStreamToOutputStreamRunnable(process.getInputStream(), System.out)).start();
final Writer stdin = new OutputStreamWriter(process.getOutputStream());
stdin.write("xemacs&\n");
stdin.write("cd ~/work\n");
stdin.write("dir\n");
stdin.write("ls\n");
stdin.write("gobbldygook\n"); // Forces output to stderr
stdin.write("echo $PATH\n");
stdin.write("pwd\n");
stdin.write("df -k\n");
stdin.write("ifconfig\n");
stdin.write("echo $CWD\n");
stdin.write("dir\n");
stdin.write("cd ~/work/jlib\n");
stdin.write("dir\n");
stdin.write("cat /etc/bash.bashrc\n");
stdin.close();
final int exitVal = process.waitFor();
System.out.println("Exit value: " + exitVal);
}One can use the same approach with Windows using cmd.exe but then one must be aware of the syntactic differences between commands running in .bat file and command run from the command line. Reading 'help cmd' s essential here.
The class PipeInputStreamToOutputStreamRunnable in the above example just copies an InputStream to an OutputStream and I use
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class PipeInputStreamToOutputStreamRunnable implements Runnable
public PipeInputStreamToOutputStreamRunnable(InputStream is, OutputStream os)
is_ = is;
os_ = os;
public void run()
try
final byte[] buffer = new byte[1024];
for (int count = 0; (count = is_.read(buffer)) >= 0;)
os_.write(buffer, 0, count);
} catch (IOException e)
e.printStackTrace();
private final InputStream is_;
private final OutputStream os_;
} -
Running ssh command from java and then answering password prompt
Hi,
I have a situation that has not solved yet. I am running ssh command from unix terminal without any problem, and then i enter password.
For example :
[oracle@fuata]:/export/home/oracle> ssh -N [email protected] -L 9901:127.0.0.1:9999
Password:
It is working. I have question that how can i perform this in java? I am thinking that i can run ssh command by using Runtime Class, it is ok. But how can i answer the password? I am a bit confused. Is there any example looks like this?
Thanks for responses.futi wrote:
Thanx. Firstly i insisted to do this without jsch but actually this is harder than jsch. I edit some of code pieces PortForwardingL.java and could run it. It works problem-free. Could you say why you "insisted" on this approach. It can't be for speed+ since jsch is very fast. It can't be for portability+ since jsch is portable but the use of Runtime.exec() requires the installation of ssh software. It can't be because of limitations+ since jsch is a fully featured library. It can't be for security+ since jsch is secure. It can't be for ease of use+ since jsch is much easier to use than ssh with Runtime.exec(). Unless it's a licensing issue, it can't be for commercial+ reasons since jsch is free. The only reason I can think of why one would "insisted" on this approach is if it is for some college project. -
Running unix command using java shows error
Hi All,
I am trying to run UNIX move command using Java exec method but its throwing error, unable to rename. Here below is the code i am trying to run to move all files from one directory to another directory. source and destination directory exists.
public class Demo
public static void main(String args[])
try
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("mv /home/demo1/* /home/demo2");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
t.printStackTrace();
}if i give "mv /home/demo1 /home/demo2" in exec method, its working, with * character its giving problem.
Please help me in resolving the problem.
Thank youCharacters like *, >, &, |, etc. are interpreted by the command shell--that is by the bash, zsh, etc. programs. When you execute a command with ProcessBuilder or Runtime.exec, you're not going through one of those shells, so their interpretation of those characters is not available.
In your code, the character * is being delivered directly to the mv command (which doesn't think * is anything special), as opposed to being turned into a list of files and directories as it would be when it's interpreted by the shell. The mv command doesn't know anything about the * character being special.
If you want to have those shell interpretations, you need to execute the command through a shell. One example is like so, but a) you'll want to read up on the methods in exec() that take arrays of String, and b) you'll want to read up on ProcessBuilder, and c) you'll need to check your shell's man pages to see the exact syntax and arguments.
runtime.exec("/bin/bash -c 'mv x/* y'"); -
Running System Commands using Java
HI,
I am developing an application using java which requires some system commands to be run.For example i have to write a java function which can program the windows scheduler to run a particular executable at some time & another one to initiate an ftp.I however do not know how to execute the corresponding commands from java.Is there any way or some specific api(similar to the system command in c) that i can use to perform these operations.I am using j2sdk 1.4.0_01 on a win 98 machine to develop the application.See [url http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html]java.lang.Runtime
-
Running Unix command through Java
Hi
I am trying to run the unix command "rf filename" through Java and it doesnt seem to work.
Can anyone help in this case please
String cm = "rm ";
String delFile = args[0]; // this path is /data/temp/filename.doc
Process p = Runtime.getRuntime.exec(cmd +delFile);Also since i dont have access to delete the file through my login i always login as root for a few commands.
Is there a way i can specify user name & password & then run the command?
Please let me know
ThanxPlease discard the above msg i got a solution by just adding file.delete
thanx -
Hi ,
I would appreciate if anyone could tell me how to run DOS command such as "del" using JAVA language .Thank you.<steps onto soapbox>
Surely for something like 'del' we should be advocating a non-OS specific method so we don't lose sight of Java's cross platform abilities.
If it has to run an OS specific thing fine, but please look for a non OS specific solution first.
<steps off soapbox> -
Running a command from java code
hi all,
There is a command
"java -jar selenium-server.jar -interactive"
which i am running through command prompt after going to D:\MyFolder\Examples .
i want to execute this command using java code .please helpsubratjyetki wrote:
please answer in detail or if posible can u give the code for thisOnce more -
The detail is given in the reference. Why don't you read it?
I could give you the code but it will cost you �100 per hour. -
Problem while running dos command from java program
Dear friends,
I need to terminate a running jar file from my java program which is running in the windows os.
For that i have an dos command to find process id of java program and kill by using tskill command.
Command to find process id is,
wmic /output:ProcessList.txt process where "name='java.exe'" get commandline,processid
This command gives the ProcessList.txt file and it contains the processid. I have to read this file to find the processid.
when i execute this command in dos prompt, it gives the processid in the ProcessList.txt file. But when i execute the same command in java program it keeps running mode only.
Code to run this command is,
public class KillProcess {
public static void main(String args[]) {
KillProcess kProcess = new KillProcess();
kProcess.getRunningProcess();
kProcess = new KillProcess();
kProcess.readProcessFile();
public void getRunningProcess() {
String cmd = "wmic /output:ProcessList.txt process where \"name='java.exe'\" get commandline,processid";
try {
Runtime run = Runtime.getRuntime();
Process process = run.exec(cmd);
int i = process.waitFor();
String s = null;
if(i==0) {
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println("--> "+s);
} else {
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
while ((s = stdError.readLine()) != null) {
System.out.println("====> "+ s);
System.out.println("Running process End....");
} catch(Exception e) {
e.printStackTrace();
public String readProcessFile() {
System.out.println("Read Process File...");
File file = null;
FileInputStream fis = null;
BufferedReader br = null;
String pixieLoc = "";
try {
file = new File("ProcessList.txt");
if (file.exists() && file.length() > 0) {
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
} else {
System.out.println("No such file");
} catch (Exception e) {
e.printStackTrace();
return pixieLoc;
} when i remove the process.waitFor(), then while reading the ProcessList.txt file, it says "No such file".
if i give process.waitFor(), then it's in running mode and program is not completed.
Colud anyone please tell me how to handle this situation?
or Is there anyother way to kill the one running process in windows from java program?
Thanks in advance,
SathishHi masijade,
The modified code is,
class StreamGobbler extends Thread
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
this.is = is;
this.type = type;
public void run()
try
InputStreamReader isr = new InputStreamReader(is, "UTF-16");
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
ioe.printStackTrace();
public class GoodWindowsExec
public static void main(String args[])
try
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 95" ) )
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
} else {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
System.out.println("Executing.......");
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
t.printStackTrace();
}when i execute the above code, i got output as,
Execing cmd.exe /C wmic process where "name='java.exe'" get commandline,processid
and keeps in running mode only.
If i execute the same command in dos prompt,
CommandLine
ProcessId
java -classpath ./../lib/StartApp.jar;./../lib; com.abc.middle.startapp.StartAPP 2468
If i modify the command as,
cmd.exe /C wmic process where "name='java.exe'" get commandline,processid > 123.txt
and keeps in running mode only.
If i open the file when program in running mode, no contents in that file.
If i terminte the program and if i open the file, then i find the processid in that file.
Can you help me to solve this issue?
Maybe you are looking for
-
Hi, I am looking cost component split in copa document , Price for FERT(FG) materil valuation will at manufactured plant level is Standard price(S) and Depo level Moving avg price(V) i am doing standard cost estimate for that i have done ..setting. 1
-
Hi Experts, We have developed two smartform programs for Goods Receipt of Purchase Order and Stock Transport Order. The movement type remains same for both these transactions i.e 101. We've already Created two Output Types namely ZWE01, ZWE02 and ass
-
Kdl40s5100 picture cuts out for about 1 minute then comes back
when i'm watching programs the pictuture (and sound) will cut out then return in about one minute... this will repeat itself about every 30 -45 minutes.... problem does not occur when watching videos - only cable or antenna
-
Forms 9 and JVM hangs when scrolling
Hello! i have a big problem with forms 9.0.4 on windows. In my form i use multirecord-blocks or lovs with many records e.g. 500 or 1000. If the user scrolls down in any way (mouse or keyboard), forms and the jvm completly hangs. Even the Java-Console
-
How to get rid of "Waiting for Activation" on old iphone 3G?
Ok I just upgraded to the new 4 & planned on giving my old 3G to my son to play games, music, photos etc. I'm in the process of re-syncing it with everything for him But there seems to be a message on the screen "Waiting for Activation, this may take