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();
........
Similar Messages
-
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");
************************************************************************************************************************************** -
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? -
Please help How can i run Os commands thru Java programs
Hey,
I want to stop and restart the linux server thru java program.Is it possible to run the os commands thru java program.
I had it thru Runtime.getRuntime().exec("*.exe");
it only runs the exe files.How can run files other than exe files like .bat,com ans shell commands..Any body knows please help with the code..or mail to this address
[email protected]
thankyou,
regards,
j.mouliWhat about "start command.com /C execute.bat", or using the overload that takes a String[] as argument?
What if you use the the full path of execute.bat?
What error code do you get?
And what comes th linux, I'm not sure... you'll need a shell interpreter there too, me thinks. (never had to run anything with runtime.exec on linux). Check the man pages if csh, bash, ksh, or what ever shell you like. -
Run Basic Command-line Java Programs in Sandvox
Is it possible to put a command-line Java program on a website in Sandvox and make it run?
See: documentation for designers
-
Run Command through Java Program.
I am giving ping command in exec method of Runtime().
But ping syntax is different for linux and windows. So when I run command on windows it wasn't worked. I am giving command --> ping -c5 ipaddress . This works fine for linux but in windows, there is no -c option. So is there any solution to overcome this situation?
please reply ASAP.
Thanks,
RajI am writing code that runs ping command on all OS.
Ping syntax different for different OS (like
windows,linux,solaris etc).
I don't want to check for each OS and run command
specific to that OS.
I want to write a generic program so ping command
execute regardless of which OS. This is self-contradictory. You can't use one command line string on different platforms if they don't support this one command string.
The ping command is different (concretely, this -c option you mention is indeed not supported on Windows - what does it do anyway?) on different OS.
So, you basically have two options:
- You use only a subset of the ping options, one that does run on all your platforms (I actually expect ping to be pretty portable anyway but...).
- Or you do use platform specific options like -c, in which case there's no way around checking the running OS and generating a specific command line.
Can I use system calls in my program to execute the
ping command, If yes, how?Rephrase please. -
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. -
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 -
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_;
} -
How to run the jmeter through java program
i wnat interact jmeter through my java program
i will pass values to jmeter through my java program
i wnat retrive the results from jmeter to my java programRead the manual.
-
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 -
How to run solaris commands through java code ....
Hi,
actually i want to run some solaris commands for zipping some files on Solaris OS...
any idea how can i do that ?
thankspublic class TABLES
public static void main( String[] args )
//database is connected
try
Connection con = null;
Statement stmt = null;
String strShowTables = "";
ResultSet resultSet = null;
// CBA Statistics period is m_lStatisticsPeriod minutes
con = DriverManager.getConnection( g_strRWURI, g_strRWUsername, g_strRWPassword );
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("use db");
resultSet = stmt.executeQuery(strShowTables);
String tableName = "";
while(resultSet.next()){
tableName = resultSet.getString(1);
System.out.println(tableName);
break;
String strCmd = "tar cvzf file.tar.gz var/lib/mysql/db/GROUPS.*";
Process p= Runtime.getRuntime().exec(strCmd);
System.out.println(strCmd);
stmt.close();
rs.close();
resultSet.close();
con.close();
catch ( Exception e )
System.out.println( ": Failed to create database connection (" + e.getMessage() + ")" );
e.printStackTrace();
catch ( Throwable t )
System.out.println( " Throwable: " + t.getMessage() );
t.printStackTrace();
}//end of main mehtod
}//END OF CLASSi hava tried the above code... what the problem is
when is run that command on shell > tar cvzf file.tar.gz var/lib/mysql/db/GROUPS.*i works fine but in code even though it didn't give any error but the created "file.tar.gz" is empty...
Edited by: aftab_ali on Apr 7, 2009 7:15 AM
Edited by: aftab_ali on Apr 7, 2009 7:17 AM -
Accessing RUN through java programming
Sir,
i need to develop a java application t access "run" in start menu.How can i access the run menu through java programming.?I want to run the program in the run command when we input command through the java program..please help me..if you have the code palese send it tio me please..........But I cant access the drives for eg: I wrote c:\programfiles as input but i got an exception like this
error=3
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at java.lang.Runtime.exec(Runtime.java:326)
at ex.main(ex.java:10)
how can correct this error... -
Setting java runtime option through java program
I want to set java runtime options(like -verbose, -ea) through java program instead of setting them through command promt. (Like, I can set -D options through System.setProperty() method). Is there any way out?
I want to set java runtime options(like -verbose,
-ea) through java program instead of setting them
through command promt. (Like, I can set -D options
through System.setProperty() method). Is there any
way out?I don't think you can (at least not for the Sun JVM).
The command-line options for the JVM are used when the
JVM is created, which precedes the loading of the class
files representing your application. At that time it is too
late to set the start-up options for the JVM which is already
up and running. -
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
Maybe you are looking for
-
ESS pages coming in enlish in Chinese language
Hi All, The ESS page (personal information, travel & expense) is coming in English though the language is changed to Chinese. The page is part of a custom role which is copied from standard employee self service role. Please help me to resolve the i
-
Problems with deploy on OracleAS/OC4J 10.1.3.2
Hi all, I was successful to deploy 2 EJB-JARs and one EAR/WAR into oc4j 10.3.2.0 stand-alone, and all this piece communicate via EJBs. I copied the EJB-JARs into j2ee/home/applications directory and modified the j2ee/home/config/application.xml to ad
-
Dear all, i am facing a problem in sales order. i explain the process, if i am creating sales order with reference to quotation will take all item from quotation. there is one condition in pricing procedure ZFR1(excise factor) on this price we calcul
-
FPGA error when opening reference
I am running the 8.6 and all the latest versions of the software on a cRIO device. In my RT code i am opening a reference to an FPGA file and when I do it times out with this error: error code: -63192 in Host CRIO Kyle.vi in Host CRIO Kyle.vi<append
-
Characteristic restricted DP interactive Planning
Hi !!!!! When I try on my interactive planning, view my table for a object i have a mistake. The message is : - Superior characteristic 0DIVISION of characteristic ZBGPCUSSL has to be restricted - Error reading data - Planning book cannot be processe