I am trying to run a powershell command which starts the tomcat service.Currently the command is working perfectly when executed directly through the windows powershell. However if i run the same command from java i get and error saying
Start-Process : A positional parameter cannot be found that accepts argument 'net'.
my powershell command is:
Start-Process -verb runas cmd -ArgumentList "/k net start Tomcat7"
my java code:
final String PS_COMMAND = " powershell.exe Start-Process -verb runas cmd -ArgumentList /k net start Tomcat7 " ;
Process p= Runtime.getRuntime().exec(PS_COMMAND);
BufferedReader BR=new BufferedReader(new InputStreamReader(p.getInputStream()));
String l;
while((l=BR.readLine()) != null){
System.out.print(l);
}
I see now you're using -verb runas to force a UAC prompt if needed.
In that case, it's a good approach but at least you can remove the cmd.exe indirection. The reason it isn't working as is though is really just because you didn't quote the values you're passing to the -ArgumentList parameter.
Try this:
final String PS_COMMAND = "powershell.exe Start-Process -verb runas net.exe -ArgumentList 'start Tomcat7'" ;
You are complicating things too much. There's no reason to run powershell.exe to run cmd.exe to run net.exe.
You should do one of the following:
Run PowerShell's command:
final String PS_COMMAND = "powershell.exe Start-Service Tomcat7" ;
Or run net.exe directly:
final String PS_COMMAND = "net.exe start Tomcat7" ;
Related
I have a powershell script that is calling a jar via the following code:
Start-Process java -ArgumentList '-jar', "$jarPath", "$csvPath"
However, the output from the jar is not coming through. I'm pretty sure its running successfully, but I'd like to be sure. How can I pass it through to the Powershell console?
Replace Start-Process with the call operator:
& java -jar $jarPath $csvPath
This works fine for me:
$stdout = "C:\temp\stdout.txt"
Start-Process powershell -ArgumentList "echo 123" -RedirectStandardOutput $stdout -Wait
$output = Get-Content $stdout
echo $output
Remove-Item $stdout
Since I started Powershell process with command echo 123, it returned 123to stdout, so this value is saved to file.
Swap Powershell with Java and it should be working as you expect.
Remember, that you cannot redirect stdout directly to variable, you must do it via file.
I'm trying to run console program inside windows cmd terminal. It works. But program which I run don't added to history.
String[] command = {"cmd.exe", "/C", "\"start cmd.exe /K \"" + commandLine + "\"\""};
ProcessBuilder builder = new ProcessBuilder().command(command);
start = builder.start();
I want to add ability to users to easily rerun this program lately.
Is there any way to add command to cmd.exe history?
I am trying to execute a powershell script from my java console app, I was able to get this working with the below command:
Process p = Runtime.getRuntime().exec("cmd.exe /c start cd "+dir+" & start cmd.exe /k \"Powershell C:\\runscript.ps1 args\"",null,dir);
p.waitFor();
Inside my powershell script, i have the below snippet which gets called in a loop,
Start-Process -FilePath $RunLocation -ArgumentList $args -wait -NoNewWindow -RedirectStandardOutput $OutputFile
$output = Get-Content $OutputFile| out-string
if(!($output.toLower().contains("failed"){
Remove-Item $outLogFile
}
If I open command prompt, and copy exactly what I have in my exec(...) command, it runs great, however, when I run it in my Java application, it seems like the -wait in my powershell script is being ignored, and the next line (which is checking and removing logs) is run, I've even gone to the length of adding a sleep for a few seconds in my powershell just after the Start-Process, this works but I'm hoping there is a better way.
The error i am getting is in the powershell script is below (this only happens when ive run it from my Java app, the -wait waits foor the start process to finsih before continuing when run directly from command prompt..):
Remove-Item : Cannot remove item D:\adhoc\logs\2016-06\output38844448.out: The process cannot access the file
'D:\adhoc\logs\2016-06\output38844448.out' because it is being used by another process.
At C:\runscript.ps1:91 char:3
+ Remove-Item $OutputFile
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (D:\adhoc\log...4448.out:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Why is the -wait in my powershell script not working when I run it from my Java app using runtime().exec?
I want to use "adb logcat -d > C:\Users\lenovo 01\Documents\android\sdk\platform-tools" command line command within my java code. this works directly in command prompt but it doesn't work within java code.
for example:
pb = new ProcessBuilder("adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
pc = pb.start();
pc.waitFor();
System.out.println("written");
when I execute this, nothing happens. It writes only "written" but the file is empty. When I run this command in command prompt, it works correctly and writes all logcat output to that file. What am I doing wrong?
Redirecting output to a file is a feature of the command interpeter; it's not something that can be performed by the process itself. Try appending cmd /c to the beginning of your command:
pb = new ProcessBuilder("cmd", "/c", "adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn't work, and I need to set chmod to 775 or else I don't have permission to read logs.
Why doesn't this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();
Since you say chmod 775 for log file it works, it's obvious your process doesn't have permission.
You can run your java with sudo:
sudo java ClassFileName
Or just add sudo as the first string in the array that you pass to bash process:
command[0]="sudo -u oracle ";
//command[1]=commandname;
//command[2...n]=Other params;
Assuming user oracle is in sudoers list and won't ask for password, this will run just like how it runs in commandline when you use sudo.
a. You don't need the bash -c, when you're executing the command you have a shell.
b. The command needs to be split on spaces and then passed into the ProcessBuilder as an array.