Running a shell command in windows
Hi All,
I am facing trouble trying to run the unix command in windows. I know that Runtime.getRuntime().exec(command) has to be used for this purpose. I have a slightly different requirement , i.e it is not simply executing the command.
Before I execute a command say " export $KEY_SETTINGS " I need to invoke the mks shell and then execute a shell (which sets few variables).
So the code which is written (not working though :( ) is
osCommand = "export $KEY_SETTINGS "
pathMKS = "C:\\temp1\\mksnt\\mksnt\\sh.exe";
pathENV = "C:\\\\temp1\\\\temp2\\\\Environ.sh";
command[0] = "cmd";
command[1] = "/c";
command[2] = pathMKS + " -c " + "\". " + pathENV + " ; " + osCommand + "\"";
report.println("Comanda="+ command[0] + ' ' + command[1] + ' ' + command[2]);
Process process = Runtime.getRuntime().exec(command);
bufferedreader = new BufferedReader(new InputStreamReader(process.getInputStream()));
bufferedreaderErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
for(int i = 0; (s = bufferedreader.readLine()) != null && i < 1; i++) {
commandOut = commandOut + s + "\n";
report.println("Print env:" + s);
while((s = bufferedreaderErr.readLine()) != null) {
commandErr = commandErr + s + "\n";
if (!commandErr.equals("")) {
throw new RuntimeException("Error running command: " + command[2] + "\n. Error: " + commandErr);
The error I am getting is
"Error running command: C:\oracle\mksnt\mksnt\sh.exe -c ". C:\\oracle\\appl\\Apps.sh ; export $KEY_SETTINGS "
Error: Export: not found.
Please help me out in this.
Best Regards
Shrey
Resending it again
Similar Messages
-
Run a shell command using Pl/Sql
hi all
i wonder if anyone knows a way to run a shell command using pl/sql
other than java stored procedure
as it seems not to be working in my case
thanx in advance,
Rashaofcourse not
i sent it once then i've got disconnected from interent then i reconnected
and resend my question so it was sent twice
now i hope you can answer my question !!!
Do you really think when asking twice or more often you will get a quicker answer? -
How to Execute Power Shell Command From Windows Application.
Hi All Experts,
I want to execute power shell commands from my windows application.
Is it possible ?
Please let me know your comments..
Thanks.I have not tried this, but apprently it is easy as in the sample pasted below. please see this article for more details
http://geekswithblogs.net/Norgean/archive/2012/09/19/running-powershell-from-within-sharepoint.aspx.
Let me know how it goes...
public string RunPowershell(string powershellText, SPWeb web, string param1, string param2) {
// Powershell ~= RunspaceFactory - i.e. Create a powershell context
var runspace = RunspaceFactory.CreateRunspace();
var resultString = new StringBuilder();
try
// load the SharePoint snapin - Note: you cannot do this in the script itself (i.e. add-pssnapin etc does not work)
PSSnapInException snapInError;
runspace.RunspaceConfiguration.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out snapInError);
runspace.Open();
// set a web variable.
runspace.SessionStateProxy.SetVariable("webContext", web);
// and some user defined parameters
runspace.SessionStateProxy.SetVariable("param1", param1);
runspace.SessionStateProxy.SetVariable("param2", param2);
var pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(powershellText);
// add a "return" variable
pipeline.Commands.Add("Out-String");
// execute!
var results = pipeline.Invoke();
// convert the script result into a single string
foreach (PSObject obj in results)
resultString.AppendLine(obj.ToString());
finally
// close the runspace
runspace.Close();
// consider logging the result. Or something.
return resultString.ToString();
Ok. We've written some code. Let us test it.
var runner = new PowershellRunner();
runner.RunPowershellScript(@"
$web = Get-SPWeb 'http://server/web' # or $webContext
$web.Title = $param1
$web.Update()
$web.Dispose()
", null, "New title", "not used");
-Sangeetha -
Running a shell command when UPS power is active.
I am hoping someone can help me create a shell command that will execute an automator workflow to send an email when my server has switched to UPS power.
I've created an automator workflow that opens mail, creates an email, and sends it. Now I need to create a way to execute the workflow when the server switches from AC power to UPS power.
Is this whole setup even possible? I have never written apple shell commands before so that is where I need the most help.
Thanks!Resending it again
-
Issues with running UNIX shell command from Java
Here is my java class file:
create or replace and compile java source named host as
import java.lang.* ;
public class Host
public static void cmdTest()
String cmd = "ps -ef | grep orcl" ;
int rc = 0 ;
int = runCmd(cmd) ;
public static int runCmd(String str )
Runtime rt = Runtime.getRuntime() ;
int rc = -1 ;
try
Process p = rt.exec(str) ;
int bufSize = 4096 ;
BufferedInputStream bis =
new BufferedInputStream(p.getInputStream(), bufSize) ;
int len ;
byte buffer[] = new byte[bufSize] ;
// Log what the program spit out
while ((len = bis.read(buffer, 0, bufSize)) != -1)
rc = p.waitFor() ;
catch (Exception e)
e.printStackTrace();
rc = -1 ;
finally
return rc ;
show errorsI can call runCmd directly from SQLPlus using an Oracle stored procedure and it runs like expected. I can see the returned output using dbms_java.set_output(1000000).
But when I call cmdTest from SQLPlus using an Oracle stored procecdure it just hangs forever and I have to kill the session.
Is there something I am missing in the cmdTest method?Jimmy,
If you haven't already seen it, I think the following article may be of help:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Also a "finally" block should not contain a "return" statement:
http://weblogs.java.net/blog/staufferjames/archive/2007/06/_dont_return_in.html
Good Luck,
Avi. -
How to run 'Get-AssignedAccess' or 'Set-AssignedAccess' power shell commands in c# Application
Hi,
I have console application using which i am trying to run power shell command like 'Get-AssignedAccess' or 'Set-AssignedAccess'.
i am using below code for this it is throwing exception 'Get-AssignedAccess' doesn't exist in cmdlet which is correct because these commands belongs to function category.
using (PowerShell pwInstance = PowerShell.Create())
pwInstance .AddScript("Get-AssignedAccess");
var result = pwInstance .Invoke();
How can we execute this kind of command using c#?
Thanks,Hi prakashlight,
Thank you for comming back and tell us the result. For more information about how to run PowerShell script in C# language, you can refer to this blogpost here:
Executing PowerShell scripts from C#
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey. -
How can I run a dos command from java on windows 98, 95?
The usage of cmd.exe in the java program to run a dos command on windows 98. 95 doesn't work as the equivalent command is command.exe
But using the command.exe in the java program makes my program to hang.hi,
As u mentioned, u cannot use the cmd.exe in win9x environment as cmd.exe is specific to windows NT, you can use the command.exe without any hitches.
for eg
java.lang.Runtime.getRuntime().exec("start command /K a.bat"); should run the batch file a.bat..
if the problem persists, try posting the snippet of code that you are using.
cheerz
ynkrish -
hi all
i wonder if anyone knows a way to run a shell command using pl/sql other than java stored procedure
as it seems not to be working in mycase
thanx in advanceSatish,
Would you be kind enough to share some sample code for us? I am interested to see that.
My take on it was exactly as Richard's, I would do it the other way round, wrap PL/SQL in UNIX and I could accomplish everything that you are mentioning like doing ftp, running SQL* Loader etc..
There may be specific situations where they may need the opposite, that is why I am interested to see the code.
Thx,
Sri -
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 shell commands in pre-post session commands
Hi Nikita: It depends on what kind of OS your Secure Agent is running on. If you're running your Secure Agent on Windows, you can enter DOS commands into these fields which may include SQL scripts using batch files. Similarly, if you're running your Secure Agent on Linux you can enter shell commands which may include SQL scripts using shell scripts. You should be able to find some examples of these commands from other Informatica Cloud users by searching the Community. Hope this helps,Josh
Hi all,I am new to informatica, can anyone please explain me how should i run shell commands in pre/post session commands?and what type of shell scripting is usually needed for the informatica jobs? Thank you
-
Running Shell Commands (not Executable) in Unix from Java
What are my options to run shell commands from Java?
My goal is to change my existing shell environment variables to some new ones provided by .anotherProfile.
Using an executable from Java is not an option because it does not work i.e. ( exec(". /home/.profile") ) brings up errors.
Someone has suggested that I start a child shell with that profile and work from there, but I'm unfamiliar with that sort of syntax and programming in general.
Any good help equals duke dollars :)Well there are some possibilities. In the original thread you mentioned that you wanted the shell script to be executed to change some enviroment parameters of the shell the JVM is executing in.
If so, and you are able to rewrite the profile so you can parse it manually. Then you can change some environment setting by writing the JNI wrappers for the getenv and setenv system calls. (Check your man pages)
That will change the environment. I am just wondering what good it will do for you? What's use of sourcing the profile in a JVM? -
Shell Commands not found error when running application
Hello,
Im using MonoDevelop IDE in my Mac, before it used to work fine but after few days, now when I'm opening the application nothing happens., i checked the Console for the log and it says this
May 9 21:29:39 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x52052].com.ximian.monodevelop[1510]: /Applications/MonoDevelop.app/Contents/MacOS/monodevelop: line 17: dirname: command not found
May 9 21:29:39 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x52052].com.ximian.monodevelop[1510]: /Applications/MonoDevelop.app/Contents/MacOS/monodevelop: line 31: //mono-version-check: No such file or directory
May 9 21:29:39 Sri-Vishnu-Totakuras-MacBook-Pro com.apple.launchd.peruser.501[599] ([0x0-0x52052].com.ximian.monodevelop[1510]): Exited with code: 1
I thought may be some error with the "dirname" command, so i commented it in the code and gave the path of directory myself and then the console shows up like this.
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: /Applications/MonoDevelop.app/Contents/MacOS//mono-version-check: line 19: grep: command not found
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: /Applications/MonoDevelop.app/Contents/MacOS//mono-version-check: line 19: cut: command not found
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: /Applications/MonoDevelop.app/Contents/MacOS//mono-version-check: line 20: cut: command not found
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: /Applications/MonoDevelop.app/Contents/MacOS//mono-version-check: line 21: cut: command not found
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: /Applications/MonoDevelop.app/Contents/MacOS//mono-version-check: line 22: cut: command not found
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: /Applications/MonoDevelop.app/Contents/MacOS//mono-version-check: line 31: osascript: command not found
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: Cannot launch MonoDevelop
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro [0x0-0x56056].com.ximian.monodevelop[1532]: MonoDevelop requires the Mono Framework version 2.10.4 or later.
May 9 21:34:12 Sri-Vishnu-Totakuras-MacBook-Pro com.apple.launchd.peruser.501[599] ([0x0-0x56056].com.ximian.monodevelop[1532]): Exited with code: 1
I opened the contents of the application and run the shell script for opening aplication there it worked very fine and application started working.. But if i open application directly through launchpad or finder it doesnt show up and console
I tried running those commands in the terminal directly there they work.. But dunno whats wrong in here. and even if try to open the application through the terminal it all opens fine.. what could be the possible problem, is it with my mac..?i finally figured out the problem.. it is with the environmental variables. the path for all those commands like "grep" is not being set for the GUI applications. that is why i got that error.
for those GUI applications which require path, we have to set path in the file /etc/launchd.conf the environmental variables set in here are used by the gui applications and those which are launched through spotlight.
that file may bot be present in most of the macs. No problem even if not present,we can create one and use.
so type in terminal
$ sudo vi /etc/launchd.conf
and in the editor add these lines
setenv PATH = /new/path
and save. Note: replace /new/path with the path which you need to have for the commands used in the script.
remeber that you need to restart your mac for applications to use these environmental variables. -
LabView equivalent to running batch files using the "shell" command (VisualBasic)
I'm converting a VisualBasic app to LabView and am having trouble figuring out how to run a batch file with LabView.
The VB code that I'm trying to replicate is:
'UNLOAD RTX DRIVERS
Dim ProcessId As Long
ProcessId = Shell(App.Path + "\UnloadReloadRTX.bat", vbNormalFocus)
Wait 400
I haven't found a LabView equivalent to the Shell command. Any suggestions will be appreciated.
thanks,
ToddIt seems as if this question pops up every week. Use the System Exec.vi found under Funtions - Communications palette. It is the equivalent of Shell.
- tbob
Inventor of the WORM Global -
How to run a command line window from code?
In an application I need to run a dll file with a few arguements. These arguements can be given in the command line window. The execution of the dll file will return a string that i need to get (for the parsing etc).
1. How can I open a command line window from the Java code?
2. How to run commands on it?
3. How to get the results of the commands from the command line into my program?In an application I need to run a dll file with a few
arguements. These arguements can be given in the
command line window. The execution of the dll file
will return a string that i need to get (for the
parsing etc).
A dll is used by other programs. It is not possible to run it from the command line. -
I would like to run a unix command and capture the output by running a java program on a windows 98 machine, this will be ran on a secure intranet. What is the best way to do that. If anyone knows of an example source code that would be great.
thanks,
DeanTry this. It always works for me.
import java.io.*;
public class Exec {
private BufferedReader out;
private Process p;
public Exec(String cmd) throws IOException {
p = Runtime.getRuntime().exec(cmd);
out = new BufferedReader(new InputStreamReader(p.getInputStream()));
public BufferedReader getBufferedReader() {
return out;
public void waitFor() throws InterruptedException {
p.waitFor();
public static void main(String [] args) throws IOException,
InterruptedException {
final Exec p = new Exec("your command goes here.");
new Thread(new Runnable() {
public void run() {
try {
String s = null;
while((s = p.getBufferedReader().readLine()) != null) {
System.out.println(s);
catch(IOException io){}
}).start();
p.waitFor();
}
Maybe you are looking for
-
Could not find any content hosted eligible apps
Hi.... my X-code version is 5.0, IOS version is 7.0 I created test user and test products in in app purchase option under my application in itunes connect. I want to display content for each test products when user buy my free products. For displayin
-
Google 2 Step Verification on Gmail causing 10.9 issues with iCloud Keychain Sync?
Hi I have just installed 10.9 on my Air 2011 and it works fine. I signed up the iCloud Keychain sync. I also updated my iPhone to iOS 7.0.3 and also signed up for iCloud keychain sync. All appeared well until I set up my iMac 2009. This had the same
-
Is there a way to use a path as a selection marquee?
Is there a way to take path, closed, and select everything that falls inside? or crosses it?
-
Mac Mini problem- A black window on screen.
There is a problem on my mac mini. A black window is comming on my screen . Working of that window is to tell us what we are doing. How to remove that window.?
-
Oracle Business Rule : Unable to modify the rule at runtime
Hi I am developing simple Business rule using Oracle jdevelper 11g which displays a message based on two rules. rule1 : If amount <= 100, displays the message "Less than or equal to 100" rule2 : If amount >100 and amount less than 200 display the mes