I'm stuck on one thing i can't get solved. I have part of code, which is executed from command line like a charm. Works without any problem.
So, i will to try to call this command (same) out from C#.
That's the code i'm run from commandline.
java -Xincgc -Xmx1024m -cp
"%APPDATA%.minecraft\bin\minecraft.jar;%APPDATA%.minecraft\bin\lwjgl.jar;%APPDATA%.minecraft\bin\lwjgl_util.jar;%APPDATA%.minecraft\bin\jinput.jar"
-Djava.library.path="%APPDATA%.minecraft\bin\natives" net.minecraft.client.Minecraft "NAME"
The part i'm trying to get it in C# looks like:
proc.StartInfo.FileName = "java";
proc.StartInfo.Arguments = "-Xincgc -Xmx1024m -cp \"%APPDATA%\\.minecraft\\bin\\minecraft.jar;%APPDATA%\\.minecraft\\bin\\lwjgl.jar;%APPDATA%\\.minecraft\\bin\\lwjgl_util.jar;%APPDATA%\\.minecraft\\bin\\jinput.jar\" -Djava.library.path=\"%APPDATA%\\.minecraft\\bin\\natives\" net.minecraft.client.Minecraft \"NAME\"";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
But, nothing happens at all. Is there something i'm doing wrong?
Thanks for any help!
Expand the environment variables in the Arguments using Environment.ExpandEnvironmentVariables.
string args = "-Xincgc -Xmx1024m -cp \"%APPDATA%\\.minecraft\\bin\\minecraft.jar;%APPDATA%\\.minecraft\\bin\\lwjgl.jar;%APPDATA%\\.minecraft\\bin\\lwjgl_util.jar;%APPDATA%\\.minecraft\\bin\\jinput.jar\" -Djava.library.path=\"%APPDATA%\\.minecraft\\bin\\natives\" net.minecraft.client.Minecraft \"NAME\"";
proc.StartInfo.Arguments = Environment.ExpandEnvironmentVariables(args);
BTW - you can use a verbatim string literal to make that argument string more readable.
#"-Xincgc -Xmx1024m -cp ""%APPDATA%\.minecraft\bin\minecraft.jar;%APPDATA%\.minecraft\bin\lwjgl.jar;%APPDATA%\.minecraft\bin\lwjgl_util.jar;%APPDATA%\.minecraft\bin\jinput.jar"" -Djava.library.path=""%APPDATA%\.minecraft\bin\natives"" net.minecraft.client.Minecraft ""NAME""";
Related
I tried to pass a string representation of a python list that stores multiple paths to a Process in C# (later on, I would like to ast.literal_eval() the first argument):
ProcessStartInfo procStartInfo = new ProcessStartInfo("powershell", command);
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
When using System.Console.Out.WriteLine(command) I get the following result:
python D:\'Box Sync'\Python\PdfMerge\PdfMerge.py -f "['C:\\Users\foo\Desktop\foo_bar.pdf', 'C:\\Users\foo\Desktop\bar_foo.pdf']" -mt "i" -op "C:\Users\foo\Desktop" -on "output"
I can copy that line into my PowerShell command line and when executed it works like a charm. However, trying to execute the script results in an error:
PdfMerge.py: error: unrecognized arguments: C:\\Users\foo\Desktop\bar_foo.pdf ]
I know that the string
"['C:\\\\Users\\foo\\Desktop\\foo_bar.pdf', 'C:\\\\Users\\foo\\Desktop\\bar_foo.pdf']"
works fine with the ast.literal_eval() so I want exactly that to be passed as an argument. I also tried to use verbatim string literals but I couldn't get any to work...
Am I missing something obvious?
Thanks for your help!
I've been searching and experimenting all morning with this one and I'm stumped. I have an aspx page running in IIS and calling the following c# function. I'm trying to have it run a cmd file and return the output from the cmd file. I've experimented with the five different options in the code below:
protected String RunMyCmdFileAndGetResponse() {
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\Windows\System32\cmd.exe";
// proc.StartInfo.Arguments = #"/c echo hello"; <== 1
// proc.StartInfo.Arguments = #"/c c:\mypath\myfile_badname.cmd"; <== 2
// proc.StartInfo.Arguments = #"/c type c:\mypath\myfile.cmd"; <== 3
proc.StartInfo.Arguments = #"/c c:\mypath\myfile.cmd"; // <== 4
// proc.StartInfo.Arguments = #"/c call c:\mypath\myfile.cmd"; <== 5
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
proc.WaitForExit();
return response;
}
Cmd file c:\mypath\myfile.cmd contents are:
#echo test line 1 > c:\mypath\myfilelog.txt
#echo test line 2
This cmd file works as expected when run manually, producing myfilelog.txt and returning test line 2. When executed with the c# code:
Option 1 works - returns 'hello' response as expected.
Option 2 fails as expected, indicating myfile_badname.cmd is not recognized as a valid command
Option 3 works as expected - it returns the contents of myfile.cmd as the response - this confirms I am able to find and read the file.
Option 4 does not work - as near as I can figure, it should. It does not hang up, but also does not return any response at all, and does not execute the cmd file (no myfilelog.txt produced).
Option 5 - same results as option 4.
Note - I've also tried modifying myfile.cmd to remove line 1 (creating the log file) and only leave line 2 to echo a response. Just in case it's a permission issue creating the log file. Same result.
Any help would be appreciated!
Updated to add solution:
The answer from #MaxOvrdrv got me thinking a different way. There does indeed appear to be some kind of limitation when running Process.Start within an IIS context with UseShellExecute = false - if the primary argument is an executable file (cmd file, script file, etc), it will not run it. I tried passing SomeExample.cmd to cmd.exe, and SomeExample.js to cscript.exe.
However... I was able to trick it with a level of indirection, such that the executable file name is no longer the first argument, and it works just fine that way.
To run a cmd file:
string theResponse = RunMyCmdAndGetResponse(#"c:\somepath\mycmd.cmd");
protected String RunMyCmdAndGetResponse(string cmdPath) {
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cmd /c " + cmdPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
To run a script file:
string theResponse = RunMyScriptAndGetResponse(#"c:\somepath\myscript.js");
protected String RunMyScriptAndGetResponse(string scriptPath) {
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cscript //nologo " + scriptPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
Running a batch file or any process per say from an ASPX page is futile as IIS does not run under a true Windows User Context. Because of this, regardless of how many settings and rights you give to the user that the AppPool is running under or whatever type of config changes you make, it simply will never work. I ran into the same problem quite a while back and basically it's impossible.
See my previous questions (and comments), along with accepted answer for possible "conceptual" solution to your current problem, here:
Process.Start won't work
What about not using "cmd"?
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\mypath\myfile.cmd";
Just I want to execute the Jar file from C sharp code and get return values from jar. Is it possible?
If so give me the sample code.
I tried following thing,
string path = "C:\\Documents and Settings\\Desktop";
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "C:\\Program Files\\Java\\jre6\\bin\\java.exe";
process.StartInfo.Arguments = "-jar \"" + path + "\\simple.jar\"";
process.Start();
String s = process.StandardOutput.ReadToEnd();
here simple.jar has main method which will take the arguements and prints the passed arguemnets in console, otherwise it prints no arguements in console.
I tried above code in this line(String s = process.StandardOutput.ReadToEnd();) able to read the console values.
But I want to execute a method by passing values in jar and method will return me hashmap (collection) values(I don't know it is possible or not).
Please give me suggestions on this.
To execute a jar file in the command line use java.exe -jar <jar_name>.
Take a look here to see how do you execute a command line program and get its output in C#.
This works for me
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "java";
myProcess.StartInfo.Arguments = "-jar C:\\FileLocal\\file.jar";
myProcess.Start();
I want to run python code from C# through command Prompt.The Code is attached below
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"d:";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"cd D:\python-source\mypgms");
p.StandardInput.WriteLine(#"main.py -i example-8.xml -o output-8.xml");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);
Output :
D:\python-source\mypgms>main.py -i example-8.xml -o output-8.xml
D:\python-source\mypgms>
But nothing happened.Actually main.py is my main program and it takes 2 arguments. one is input xml file and another one is converted output xml file.
But i dont know how to run this python script from C# through command prompt. Please Guide me to get out of this issue...
Thanks & Regards,
P.SARAVANAN
I think you are mistaken in executing cmd.exe. I'd say you should be executing python.exe, or perhaps executing main.py with UseShellExecute set to true.
At the moment, your code blocks at p.WaitForExit() because cmd.exe is waiting for your input. You would need to type exit to make cmd.exe terminate. You could add this to your code:
p.StandardInput.WriteLine(#"exit");
But I would just cut out cmd.exe altogether and call python.exe directly. So far as I can see, cmd.exe is just adding extra complexity for absolutely no benefit.
I think you need something along these lines:
var p = new Process();
p.StartInfo.FileName = #"Python.exe";
p.StartInfo.Arguments = "main.py input.xml output.xml";
p.StartInfo.WorkingDirectory = #"D:\python-source \mypgms";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
Also the Python script appears to output to a file rather than to stdout. So when you do p.StandardOutput.ReadToEnd() there will be nothing there.
Why not host IronPython in your app and then execute the script?
http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx
http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application
or use py2exe to pragmatically convert your python script to exe program.
detail steps...
download and install py2exe.
put your main.py input.xml and output.xml in c:\temp\
create setup.py and put it in folder above too
setup.py should contain...
from distutils.core import setup
import py2exe
setup(console=['main.py'])
your c# code then can be...
var proc = new Process();
proc.StartInfo.FileName = #"Python.exe";
proc.StartInfo.Arguments = #"setup.py py2exe";
proc.StartInfo.WorkingDirectory = #"C:\temp\";
proc.Start();
proc.WaitForExit();
proc.StartInfo.FileName = #"C:\temp\dist\main.exe";
proc.StartInfo.Arguments = "input.xml output.xml";
proc.Start();
proc.WaitForExit();
I think the C# process class has a problem with accepting < or > characters when they are passed as paramaters.
When I call the following code, the executable returns me an error indicating that I passed more than one argument.
proc = new Process();
proc.StartInfo.FileName = this.spumux.SpumuxExe;
proc.StartInfo.Arguments = "menu.xml < menu.mpg > newmenu.mpg";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
proc.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
proc.Exited += new EventHandler(ProcExited);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
This code normally worked with every other executable I tried so far without any problems. So it's gotta do something with <, > characters
Any ideas?
The angle brackets in this case mean redirecting the input/output, which is done by cmd.exe, not by the started process.
You have two options:
call cmd.exe instead of your executable, and supply the executable as an argument (and the arguments for your exe as well)
redirect standard input/output yourself.
try "menu.xml \< menu.mpg \> newmenu.mpg". And you are adding 5 args. To make it one - do: "\"menu.xml \< menu.mpg \> newmenu.mpg\""
I'm not sure what your attempting to accomplish here. Are you trying to redirect IO with '<' and '>', or are you trying to pass these as arguments?
You can redirect IO by running CMD.exe with the /C option:
proc.StartInfo.FileName = #"C:\Windows\System32\Cmd.exe";
proc.StartInfo.Arguments = "/C \"" + this.spumux.SpumuxExe + " menu.xml < menu.mpg > newmenu.mpg\"";
I was only be able to solve this issue by creating a batch file, where I pass the arguments without "<", ">"