Running a exe (java command) through Java Code
Hi I have a program that runs on command line using java command.This program processes a text file that it takes as an argument.I have many files to process so I cannot give command for each individual file.
Is it possible that I use Java command to run this program for many files in a loop?
If yes HOW?
It has many parameters but one parameter for input file. But I think this is not a problem, I can change it as you said.Now I have tried this code:
Note:In this code I can change B.txt each time in a loop for new input file. (This is just for check)
import java.io.*;
public class Test3
public static void main(String[] args)
Runtime run = Runtime.getRuntime();
try
Process pp=run.exec("C:/Program Files/Java/jdk1.6.0/bin/java -mx300m -classpath B.txt > sample-output.txt");
BufferedReader in =new BufferedReader(new InputStreamReader(pp.getErrorStream()));
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
int exitVal = pp.waitFor();
System.out.println("Process exitValue: " + exitVal);
catch (Exception e)
e.printStackTrace();
System.out.println(e.getMessage());
} It compiles well but gives exception NoClassDefFound when I use java command to interpret it.Can u help?
Edited by: kimskams on Jun 29, 2008 7:24 AM
Similar Messages
-
Execute java commands through java code
Hi guys,
I want to run a java class from another class.
I tried like this..
Runtime.getRuntime("java C:\test2");But test2 did not run.
But I could execute this command sucessfully.
Runtime.getRuntime("javac C\test2.java");
I could see class file generated for this.
Please suggest me how to run a java class .....
Thanks in advance.Hi guys,
I want to run a java class from another class.
I tried like this..
Runtime.getRuntime("java C:\test2");But test2 did not
run.Because the JVM (the java command) does not take a file as its argument. It takes a class name. You'd want something like java -cp C:\ test2
But I could execute this command sucessfully.
Runtime.getRuntime("javac C\test2.java");Because javac, unlike java, does take files (rather than a class name) as its argument. -
Running cmd commands through java
Hi,
I am trying to run few dos commands through java using Runtime.exec("command") but I am getting following error
commmand is cd D:\CodeMerge\A\
java.io.IOException: CreateProcess: cd.. error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at createDirectory.main(createDirectory.java:24)
Basically what I am looking for is this:
1) My program is in D:\Amit\RM
2) I want to get out to D:\ go to CodeMerge\A\.
3) Create folder with curretn date as name of the folder
Hence I am doing
rTime.exec("cd../..");
rTime.exec("cd D:\CodeMerge\A\");
rTime.exec("mkdir "+date);
Can anybody tell me why I am getting this error
JountyU can run either DOS Commands or UNIX Commands thru the following Java Program
Written By BalaNagaraju Malisetti
contact: [email protected]
import java.io.*;
public class RunBdiff
public static String rununixcmd(String v_prev_path,String v_curr_path,String v_delta_path,String v_beg,String v_len)
String s = null;
FileOutputStream fos;
DataOutputStream dos;
try
int v_int_beg = Integer.parseInt(v_beg);
int v_int_len=Integer.parseInt(v_len);
int v_int_end=(v_int_beg+v_int_len)-1;
String v_pos1=String.valueOf((v_int_beg+2));
String v_pos2=String.valueOf((v_int_end+2));
String cmd1 = "bdiff ";
String cmd2=v_prev_path;
String cmd3=" ";
String cmd4=v_curr_path;
String cmd5="| grep '^>'";
String cmd6="| cut -c";
String cmd7=v_pos1+"-"+v_pos2;
String cmd8="| sort ";
String cmd9="| uniq ";
String cmd=cmd1+cmd2+cmd3+cmd4+cmd5+cmd6+cmd7+cmd8+cmd9;
String[] commands = {"/bin/csh","-c",cmd}; //in Unix
//String[] commands = {"cmd.exe","-c",cmd}; //in Windows
Process p = Runtime.getRuntime().exec(commands);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
File file = new File(v_delta_path);
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);
//Writing the output of bdiff command to Delta File
while ((s = stdInput.readLine()) != null)
dos.writeChars(s);
//Return Error Message to Calling Procedure
while ((s = stdError.readLine()) != null)
return s;
return s;
catch (IOException e)
return String.valueOf(e);
public static void main(String args[])
String msg=rununixcmd("/home/g_anil/MS_ADDRESS1.dat","/home/g_anil/MS_ADDRESS2.dat","/home/g_anil/MS_ADDRESS_DELTA.dat","1","9");
************************************************************************************************************************************** -
How to execute MS DOS command through Java program???
Dear Sir,
I want to run a MS-DOS command through my Java program. I have tried "Dir" command but no other command which takes command line args doesn't work at all. Please help.
import java.io.*;
class CommandPrompt
public static void main(String[] args)
try
File file = new File("C:/Temp/Java");
String[] cmd = {"command.com","/c","md folder"};
String[] envp = {""};
Process m;
String s = "";
m = Runtime.getRuntime().exec(cmd,null,file);
BufferedReader buf = new BufferedReader(new InputStreamReader(m.getInputStream()));
while ((s = buf.readLine())!=null)
System.out.println(s);
catch (Exception ex)
System.out.println("Exception is "+ex);
ex.printStackTrace();1. This forum is for Swing-related issues only. This question should be posted to the "Java Programming" forum.
2. Please enclose your sample code in code blocks; it's much easier to read that way. See here for how it's done: http://forum.java.sun.com/faq.jsp#messageformat
3. Please provide more information, like what error messages you got and what OS you're running. For instance, if you're running WinXP, Win2k or NT4, your command processor should be "cmd.exe", not "command.com". When I made that change, your program worked for me. -
How to run an exe file in a java program
Hi,
Can somebody tell me how to run an exe file in a java program.
Thank you!Yes, java.lang.Runtime.exec().
Read this carefully before you do:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Don't write a line of code before you're reviewed and understood the article completely. - MOD -
Problem in executing a unix command through java
hi
i'm trying to execute unix command through java
simple shell command like "ls -l >test " but i'm not able to see the result.
there are no error messages.
Code is:
import java.lang.Runtime.*;
class ExecDemo
public static void main(String[] args)
Runtime r=Runtime.getRuntime();
Process p=null;
try
p=r.exec("ls -l > test");
catch (Exception e)
System.out.println("Error executing nedit.");
}can anyone help please.get the the inputStream of the runtime object after executing the command.
now use the readLine() function until it becomes null.
egs: with reference to ur code.
InputStream is=p.getInputStream()
while(is!=null)
String s=is.readLine();
if the command don't execute try giving the full path also like /sbin/ls -l -
What is the best way to call SSH commands through Java technologies
What is the best way to call SSH commands through Java technologies
I don't think you can specify the password at the prompt using ssh. Plink has the -pw command option for passwords. What I did was except ssh to ask me and write the password programmatically through the outputStream I obtained from my Process object. I looked for "password: ", and then wrote the password with a trailing newline.
Both plink and ssh will also ask if you trust the host if it is the first time you are connecting to it. So before just writing the password, check if the program wants verification from you: normally supplying "y" or "n". This should only have to happen the first time the client connects. It looks like thisPLINK:
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's key fingerprint is:
1024 f8:43:61:4c:a2:5b:77:be:5b:a7:bb:1f:f7:79:b3:b7
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)
SSH:
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is e1:02:13:c8:52:c2:23:41:9b:5b:58:2c:18:e6:59:af.
Are you sure you want to continue connecting (yes/no)?What I did was read character by character from the inputStream until I found things of interest ... like "password: ". You only need to search for this in your method which will launch the ssh or plink Process. Once you have verifyed yourself, you can just pass commands to the Process, which is an ssh connection.
My suggestion is to read() each byte from the Process's inputStream and write it to stdout. Look and see what you need to be looking for so you can interactive with the program correctly. You can't use readLine here becuase when the ssh asks for the password it does not print a newline. I do know for sure, that ssh will simply print, "passowrd: ". Plink will print this as well if the -pw option is not supplied. -
Accessing OS level commands through Java?
Hello everyone,
Is it possible to access OS-level (Unix BSD for instance) commands through Java classes or methods?
"OS-level commands" refers strictly to Unix commands such as I/O CTL (control), Poll and Select through a file descriptor.
"Access" refers to legitimate, built-in OR illegitimate (forced by "fooling" the system) access.However, the calls that you refer to, have been implemented in java; that goes for read/write/select (java.io and java.nio). But they pass through the java api implementation, and then throught the native implementation inside the JVM. If you want to use the raw power of poll() through java, you'd have to implement it yourself in JNI. But even if you successfully implement that, you must always remember that you'd have to make corresponding java classes that serve as arguments to your call, which, inside C, you must unpack again - so there goes your speed-advantage out the window.
-
Execute dos commands through java
Hi,
Im trying to execute dos commands through java like
try {
java.lang.Runtime.getRuntime().exec("cmd /c cls");
}catch(IOException e){
System.out.println(e.getMessage());
}Not sure if its possible? however
open notepad would work
try {
java.lang.Runtime.getRuntime().exec("notepad");
}catch(IOException e){
System.out.println(e.getMessage());
}Im trying to execute a cls commands to clear screen but without luck.The question is, which shell do you want to clear?
I don't really know, but it could be that Runtime.exec executes its command in a new shell window... -
Using the "java" command in Java 1.4.1
Hello..
I don't seem to be able to run any application that should run with
the "java" command in Java 1.4.1. Everytime I use "java" I get the "java.lang.NoClassDefinitionError"
Even running this simple program doesn't work:(
package chapter10;
public class test {
public static void main(String args[]) {
System.out.println("Hello WORLD!!");
}//end class
when I run: "java chapter10/test"
I get the following message:
"Exception in thread 'main' java.lang.NoClassDefinitionError: chapter10/test"
And the sam happens to any other class when I try to run it:(
The "javac" command works fine:) but the "java" is giving me a hard time:(
I would appreciate any suggestions or answers to this problem:) THANK USee if this explanation helps.
Assume that your programs are part of a package named divelog, which is specified by the first line in each source file: package divelog;
Also assume that this directory (C:\javaT\myWork\) is part of the CLASSPATH list of directories.
Also assume that all the source files reside in this directory structure: C:\javaT\myWork\divelog\
Then a statement to compile a source file Named Divelog.java is:
C:\JavaT\myWork\>javac divelog\Divelog.java
Explanation:
Java starts looking for classes in the directory(s) listed in the classpath. In this case, one of the directories should be: C:\JavaT\myWork\ since it contains your divelog package.
Compiling
A class can either be part of a package (ie, no package statement at the top of the class.), or not.
If the class is not part of a package: Its source file needs to be in one of the classpath directories. To compile you use the command: javac SomeFile.java, from within the classpath directory that contains the file.
The class is part of a package (this case): The source file must be in a subdirectory structure that starts in one of the classpath directories. The subdirectory structure must match the package statement.
So, you generate a directory structure C:\javaT\myWork\divelog\ which is the [classpath directory + the package subdirectory structure], and place Divelog.java in it.
Then from the classpath directory (C:\JavaT\myWork\) use the command: javac divelog\Divelog.java
This creates a file, Divelog.class in the divelog directory. (The following is where people tend to get lost.) The correct name now, as far as java is concerned, is the combination of package name and class name: divelog.Divelog (note I omit the .class)
Running
To run a class that's not part of a package: From within the classpath directory that contains the class. use the command: java SomeFile
To run a class that is part of a package: From within the classpath directory (C:\JavaT\myWork\) use the command java divelog.Divelog (Note that this is analogous to the command for a class not in a package, you just use the fully qualified name) -
How to run a command prompt " command " through 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 helpThis has already been answered in your other two threads on this topic - http://forum.java.sun.com/thread.jspa?threadID=5221221&messageID=9898287#9898287 and http://forum.java.sun.com/thread.jspa?threadID=5221223&messageID=9898290#9898290.
For some reason you don't want to read the reference that tells you exactly how to do what you want and how to avoid the pitfalls - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . -
Running windows command through java code
Hello
i want to execute jar.exe through java code , i have written following piece of code , but it isn't working
ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"cmd.exe","/c","%java_home%\\bin\\jar.exe"});
Process process = processBuilder.start();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = inputReader.readLine();
while(line != null){
System.out.println(line);
line = inputReader.readLine();
does anybody knows why
Regards
Edited by: Mayur Mitkari on Mar 5, 2013 10:19 PM
Edited by: Mayur Mitkari on Mar 5, 2013 10:20 PM
Edited by: Mayur Mitkari on Mar 5, 2013 10:20 PMsorry for that , but the
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(new String[]{"cmd.exe","/c","jar"});
proc.waitFor();
int i = proc.exitValue();
this code was different from first one
and in case of Process if runtime .exec is succesful it is wainting for long time , in this case i want if the runtime.exec is succesful something should be returned
Regards -
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 -
Running command line commands through java
How do I run other programs through Java and capture their output. E.g. If I want to capture output of 'ls *.txt' what do I do ? If you can paste some sample code or point me to some tutorial that would be cool.
Thanks,I tried that
Process proc = Runtime.getRunTime().exec("curl " + query);
but now what. From the documentation I could see that
public abstract OutputStream getOutputStream()
might be helpful. But then which OutputStream class to use ? I am looking for a coding example. If you could paste it, that would be great. -
Running OpenSSL command through Java Program
How do I execute openssl commands through a java program? Any packages or wrapper classes are there? Please help.
Thanks.Hi!
What do you mean execute commands? Like: "openssl x509 -in cert.pem -out certout.pem" ??
In that case you can just try the following:
import java.lang.Runtime;
try {
Runtime.getRuntime().exec("openssl x509 -in cert.pem -out certout.pem");
}catch (Exception e) {
e.printStackTrace();
........
Maybe you are looking for
-
How can I re-charge my ipod when I think it needs re-charging?
Hi all. Now this may sound like a stupid question, but here goes. I use my ipod every day to go for power-walks. I'm not sure when the power is depleted and I don't know how to check for that (is there a way). but I've used it every day this week and
-
I have been searching this helpless website for 3 nights after work trying to figure out how to get my old phone back to "WHEREVER" it needs to go. I am soo frustrated, I'm ready to throw this phone away and forget it. I've read through posts and tri
-
Substitution rule is not working correctly
Hi PS Gurus, We have a problem in one of the newly created projects that the substitution rule is not working correctly; the data is not copied to the WBS element Cust. Enhancement tab. Project 1 is working fine and the data is copied & Project 2 and
-
Help with a "AT NEW" statement
I am debugging at some code and I am trying to understand the following statement <i>LOOP AT it_data_item_final INTO st_data_item_final. MOVE st_data_item_final-waers TO w_waers. <b>AT NEW xblnr."xblnr." budat xblnr waers.</b> MOVE-CORRESPONDIN
-
I believe this is a bug. Incompatibility with MS UAC.
I posted the problem below yesterday and then slept on it. This morning I proved that with the default UAC setting selected in Windows and using the the two disk setup I indicated below, then the filters I mention do not work. IF I reset UAC to the b