简体   繁体   English

环境变量对java不可见

[英]Environment variables not visible to java

The project 该项目

The project is a large C# project which is used for test-automation. 该项目是一个大型C#项目,用于测试自动化。 For this purpose i've to use a java-tool which is the tool which saves all results into a file which can be loaded into a test-environment. 为此,我将使用java工具,该工具将所有结果保存到可以加载到测试环境中的文件中。

The interface 界面

I got a DLL from the vendor of the test-environment which is build in C++, this dll loads the java environment and loads the jar files. 我从测试环境的供应商那里得到了一个用C ++构建的DLL,这个DLL加载了java环境并加载了jar文件。

Current situation 现在的情况

The java environment is loaded with success, its configured with environment-variables set in C# with this method: java环境加载成功,使用此方法在C#中设置环境变量:

String java = GetJavaInstallationPath();
Environment.SetEnvironmentVariable("PATH", String.Format("{0};{1}", Environment.GetEnvironmentVariable("PATH"), Path.Combine(java, @"bin\client")), EnvironmentVariableTarget.Process);

After this i set the path to the java classes using this code: 在此之后,我使用以下代码设置java类的路径:

Environment.SetEnvironmentVariable("ITEPCLASSPATH",
                String.Format("{0};{1}",
                Path.Combine(iTepPath, "itep.jar"),
                Path.Combine(iTepPath, "libs\\itorx.jar")), EnvironmentVariableTarget.Process);

Which actually should work, it shows the correct value when using Environment.GetEnvironmentVariable("ITEPCLASSPATH") but the C++-DLL tells me that it isn't working. 实际应该工作,它显示使用Environment.GetEnvironmentVariable("ITEPCLASSPATH")时的正确值,但C ++ - DLL告诉我它不起作用。

When setting the class path by using a external bat-file it works. 使用外部bat文件设置类路径时,它可以正常工作。 Some more facts: 更多事实:

  • The application is started by the bat file 该应用程序由bat文件启动
  • The path is copied from my generated path of the dll 从我生成的dll路径复制路径
  • I don't comment anything out, so the path is still set by C# 我没有评论任何东西,所以路径仍由C#设置

It seems that java is not accessing the env.-variable i set in C# but recognizes that i set it in the bat file. 似乎java没有访问我在C#中设置的env.-variable但是认识到我在bat文件中设置它。

I really need to set the variable via C#, how do i archive this? 我真的需要通过C#设置变量,我该如何存档?

It is not explicitly written in Microsoft System.Environment documentation but the target value Process seems to limit scope to the current process only. 它没有在Microsoft System.Environment文档中明确编写,但目标值Process似乎仅限于当前进程的范围。 By default, the CreateProcess method inherits current process environment for the child process. 默认情况下, CreateProcess方法继承子进程的当前进程环境。 Maybe the parameters used there breaks this default behavior. 也许在那里使用的参数打破了这种默认行为。

So I propose you test first with EnvironmentVariableTarget.User in SetEnvironmentVariable to see if it works better. 因此,我建议您首先使用SetEnvironmentVariable EnvironmentVariableTarget.User进行测试,看看它是否更好。

By the way, I think you will have to diagnose further environment variable and create process operations with tool like Process Monitor . 顺便说一句,我认为您将不得不使用Process Monitor等工具诊断更多环境变量并创建流程操作。

Make sure the environment variable works with each target: Process, User and Machine. 确保环境变量适用于每个目标:Process,User和Machine。 See this MSDN article. 请参阅此MSDN文章。

// Set the environment variable for the default target (the current process).
Console.WriteLine(fmt2x, "(default)", myVarA, existsA);
Environment.SetEnvironmentVariable(myVarA, existsA);

// Set the environment variable for the the current process.
Console.WriteLine(fmt2x, "Process", myVarB, existsB);
Environment.SetEnvironmentVariable(myVarB, existsB, 
    EnvironmentVariableTarget.Process);

// Set the environment variable for the the current user.
Console.WriteLine(fmt2x, "User", myVarC, existsC);
Environment.SetEnvironmentVariable(myVarC, existsC, 
    EnvironmentVariableTarget.User);

// Set the environment variable for the the local machine.
Console.WriteLine(fmt2x, "Machine", myVarD, existsD);
Environment.SetEnvironmentVariable(myVarD, existsD, 
    EnvironmentVariableTarget.Machine);

Java allows you to pass environment variables as parameters using the: Java允许您使用以下命令将环境变量作为参数传递:

java -DMYPROP=MYVALUE myclass.class

argument syntax. 参数语法。 Check out the -D flag. 看看-D标志。

Those system properties then apply to that JVM process instance. 然后,这些系统属性将应用于该JVM流程实例。 Wouldn't that be simpler than trying to modify the OS Environment? 这不会比尝试修改操作系统环境更简单吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM