javac#jar

Jar file classpath problems


I am developing a C# application which has access to console to run the java command and execute an specific jar, but when I run the program, the executing jar file generate its files on program output, like logs and configuration, but I want to run the jar that's already in a specific folder with its files.

The problem is that I tried many java arguments, but the generated output files from the jar goes every time to my Debug folder from my Visual Studio project.

Hope anyone understood me, I very confused and sorry for my English, I am Brazilian For now, thanks for your support!


Solution

  • 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