I am developing a C# application wich have access to console to run the java command and execute an specific jar, but when I run the program, the executing jar file generate his files on program output, like logs and configuration, but I want to run the jar that's already in a specific folder with his files The problem is that I tried many java arguments, but the generated output files from the jar goes everytime to my Debug folder from my visual studio project Hope anyone understood me, I very confused and sorry for my english, I am brazillian For now, thanks for your support!
-
2You should set the current working directory to what the invoked program expects before invoking it. – Thorbjørn Ravn Andersen Jan 25 '17 at 00:35
-
yes I just think about it, but how I set the working directory for the jar? I tried with -cp argument but no result – DJ Vickz Jan 25 '17 at 01:04
-
You must do this in the surrounding environment where you invoke the java command. – Thorbjørn Ravn Andersen Jan 25 '17 at 16:22
-
please help I am getting crazy with this, I can't modify the jar file, it's pre-compiled and is not my project, I only invoke it from my C# application, what to do? :( – DJ Vickz Jan 27 '17 at 03:22
-
3Please show the code you are using to launch the java application from C# . – Arnaud Jan 27 '17 at 10:51
1 Answers
I can only guess, because the lines of code are missing that you use to invoke the java program from within your C# program. It is also unclear what exactly you are trying to achieve. Decide between changing the "current work directory" in the child or in the main process.
child process
I assume you want to execute something like this java -jar path\to\some.jar
using code similar to this:
using System.Diagnostics;
//...
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = new string[]{"-jar", "path\to\some.jar";
p.FileName = "java.exe";
p.WindowStyle = ProcessWindowStyle.Hidden;
p.CreateNoWindow = true;
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
}
Adapted from here
Then you can just add this line
p.WorkingDirectory = "path\to";
Adapted from here
to setup your child process covered by the p
instance with a specific current work directory. This will set the current work directory for the invocation of your java
child process.
main process
To setup the current work directory for the surrounding main process add this line to your program:
System.IO.Directory.SetCurrentDirectory("some\other\path");
MSDN Documentation for SetCurrentDirectory