GlassFish Application Server uses a script, asadmin.bat, that in turns starts a JVM.
I'd like to call this script using jinterop and DCOM from Java on a remote machine. I can't find any help on this specific usage. Any help would be greatly appreciated.
I use the Windows Scripting Host Shell to execute some program or batch on a remote computer.
The code looks like:
// Create a session
JISession session = JISession.createSession(<domain>, <user>, <password>);
session.useSessionSecurity(true);
// Execute command
JIComServer comStub = new JIComServer(JIProgId.valueOf("WScript.Shell"),<IP>, session);
IJIComObject unknown = comStub.createInstance();
final IJIDispatch shell = (IJIDispatch)JIObjectFactory.narrowObject((IJIComObject)unknown.queryInterface(IJIDispatch.I ID));
JIVariant results[] = shell.callMethodA("Exec", new Object[]{new JIString("%comspec% /c asadmin.bat" )});
If you need the output from the batch you can use StdOut to read it.
JIVariant stdOutJIVariant = wbemObjectSet_dispatch.get("StdOut");
IJIDispatch stdOut = (IJIDispatch)JIObjectFactory.narrowObject(stdOutJIVariant.getObjectAsComObject());
// Read all from stdOut
while(!((JIVariant)stdOut.get("AtEndOfStream")).getObjectAsBoolean()){
System.out.println(stdOut.callMethodA("ReadAll").getObjectAsString().getString());
}
Related
i want to ask for help with opening a file from c# app with associated app.
I tried this:
ProcessStartInfo pi = new ProcessStartInfo(file);
pi.Arguments = Path.GetFileName(file);
pi.UseShellExecute = true;
pi.WorkingDirectory = Path.GetDirectoryName(file);
pi.FileName = file;
pi.Verb = "OPEN";
Process.Start(pi);
or this:
Process.Start(file);
where string file in both examples represents full path to the file trying to open. Now, everything is working well, except the (jpg) images with ACDSee app. Irfanview associations works well, MS office documents too. After trying to open the jpg image associated with acdsee it just runs the acdsee in the notification area and does not open the file.
I discovered, that in the registry CLASSES_ROOT for *.jpg images, there is an ACDSee.JPG value as associated app, and under this key there is in the shell->Open->Command a path:
"C:\Program Files\ACD Systems\ACDSee\ACDSee.exe" /dde
and I thing that this weird /dde is the reason, why i cannot open the file. I realized that in the same reg key shell->Open there is some DDEExec key entry with value [open("%1")]
For Irfan view or other checked app there is not a ddeexec, just the normal command like
"C:\Program Files (x86)\IrfanView\i_view32.exe" "%1"
that can be run from command line after swaping the %1 for file name, but I could not run the command from acdsee entry in the command line :(
So my question is, how can I set up the ProcessStartInfo object to ensure that it will run all the files as it would be in the explorer by doubleclick, the standards and this DDEExec ones? Is there something other like DDEExec that I shoul be aware of?
thanks and sorry for my EN
UPDATE: because this question still gets upvotes, I want to clarify that accepted answer works. I only had problem with old version of ACDSee and not with the Process.Start command or with the jpg extension.
Just write
System.Diagnostics.Process.Start(#"file path");
example
System.Diagnostics.Process.Start(#"C:\foo.jpg");
System.Diagnostics.Process.Start(#"C:\foo.doc");
System.Diagnostics.Process.Start(#"C:\foo.dxf");
...
And shell will run associated program reading it from the registry, like usual double click does.
In .Net Core (as of v2.2) it should be:
new Process
{
StartInfo = new ProcessStartInfo(#"file path")
{
UseShellExecute = true
}
}.Start();
Related github issue can be found here
This is an old thread but just in case anyone comes across it like I did.
pi.FileName needs to be set to the file name (and possibly full path to file ) of the executable you want to use to open your file. The below code works for me to open a video file with VLC.
var path = files[currentIndex].fileName;
var pi = new ProcessStartInfo(path)
{
Arguments = Path.GetFileName(path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(path),
FileName = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe",
Verb = "OPEN"
};
Process.Start(pi)
Tigran's answer works but will use windows' default application to open your file, so using ProcessStartInfo may be useful if you want to open the file with an application that is not the default.
I was wondering if it's possible to execute a bat file i created via SAS?
Reason for this is, unfortunately I do not have admin privileges to schedule the execution of my batch file on task scheduler when I'm not logged in...I need to be logged in for the batch file to execute.
On the contrary my team schedules jobs on SAS so I was wondering if there is a command that can execute my .bat file through the same concept?
I'm happy to accept any other solutions
Thanks.
I always suggest using a pipe fileref when calling OS commands, this way you can capture both STDOUT and STDERR.
data _null_;
infile "C: & C:\path\to\your.bat 2>&1" pipe;
input;
putlog _infile_;
run;
To explain the command:
1) First, change the drive (not necessary if SAS is on the same drive as the .bat file). This is the C: part.
2) Next, execute the file (C:\path\to\your.bat)
3) Finally, redirect STDERR to STDOUT (the 2>&1 part). This will show you any errors.
If you still get nothing, try executing the batch file manually using the same account as you use in SAS, and - on the same machine (eg where the SASApp server is hosted). In a batch environment this may be a batch user. In a Stored Process context it may be sassrv (or equivalent).
options noxwait noxsync;
x 'path\to\your\file.bat';
I have a batch file which is used to run mstest.Clicking on the batch file executes the file just fine. However when the same file is called from a win form application, mstest fails. This behaviour seems quirky.Could anyone provide any reasons for this.
I have used the following code to call it :
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(#"D:\CodedUI\CommonAutomationFramework\Driver_batch.bat");
myProcessStartInfo.UseShellExecute = false;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
What exactly is failing? The test(s) or MSTest itself?
Anyway a batch file executes commands on the command line interpreter (cmd).
In the process you're starting, maybe you should start 'cmd.exe' instead. Read the contents of the batch file and pass them a an addiction to 'cmd.exe'
Like this:
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
(...)
Where your 'command' here is the text in the batch file.
You could also try this:
System.Diagnostics.Process.Start(#"D:\CodedUI\CommonAutomationFramework\Driver_batch.bat");
I am needing to run a remote script on our network to import files. I have set up plink and have it working as needed if I run it manually.
plink name#localserver -ssh -i myKey.ppk /home/here/scriptName.sh
We are writing the code in ColdFusion so this will run in a CFThread using CFExecute.
The cfexecute does not error when I run it via the code it just not fire the script.
In my research I have found people saying that cfexecute has some issues with the argument string and a better idea is creating a batch file and using cfexecute to run the batch file.
so I have created a batch file.
import.bat
C:\inetpub\wwwroot\myapp\plink\plink.exe name#localserver -ssh -i myKey.ppk /home/here/scriptName.sh)
again if I run the the batch file manually it works.
import.bat
but if I run it via cfexecute it does nothing.
To test cfexecute I have it running two commands, the first what I need to work and the second a test. the second works as needed. the first one is not erroring to screen or log file. It did if I entered bad syntax. The second is writing to file as needed.
(code below)
starting
<cfoutput>
<cfexecute name = "C:\inetpub\wwwroot\myapp\plink\import.bat" errorfile="C:\inetpub\wwwroot\myapp\logs\#timeformat( now(),"HHMMSS") #.log" ></cfexecute>
<cfexecute name = "C:\WinNT\System32\netstat.exe"
arguments = "-e"
outputFile = "C:\Temp\#timeformat( now(),"HHMMSS") #.txt"
timeout = "1">
</cfexecute>
</cfoutput>
the end
<cfabort>
any thoughts would be greatly appreciated...
Thanks,
Brian
Try
<cfexecute name="c:\winNt\system32\cmd.exe"
arguments="/c C:\inetpub\wwwroot\myapp\plink\import.bat" timeout="100">
</cfexecute>
I have an msi installer that needs to call a few batch files to finish the install procedure. The batch file copies extra files from the installer to a few directories and then modifies permissions on several of those directories. We want to continue using the batch files because there is not a lot of time left in our development schedule. I am not using WIX.
If possible I would like to capture the output of the batch as it goes and write it to a log file.
Found bellow is the code I am using to try to run the batch file from a custom action. It opens a cmd window, runs for a while but never seems to finish. If I run the same batch files directly from the command prompt they work.
//Set the environment to the directory containing the bat files
ProcessStartInfo info = new ProcessStartInfo(batch);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
if (!string.IsNullOrEmpty(argument))
info.Arguments = argument;
Process process = new Process();
process.StartInfo = info;
process.Start();
// Capture the standard error and standard output
What am I doing wrong?
I believe you'll need to create a custom action. See this question.
Many anti-virus programs may stop execution of .BAT files during an installation process, you should really be doing this using standard Windows Installer functionality or as a C++ Custom Action