1

What i already have:

Using eclipse keppler i am creating an eclipse plugin that is to work under linux, windows and mac. I am trying to wrap some existing java code into this eclipse plugin. This works fine. The execution of the wrapped code can be started by clicking a button in the plugin ui. The wrapped code uses some environment variables. When these are set as environment variables and eclipse is then started the variables are found and used.

What i would like to do:

I would like to set the required environment variables in some textfields in the plugin ui and then start the execution of the wrapped code which should then use the variables form the textfields, so that i no longer need to set them as environment variables before starting eclipse. This would obviously be more flexible. This feature is used by the eclipse ide itself when starting a run/debug-process. For these processes the environment variables can be set. This is the mechanism i would like to use.

My questions:

  • How is it possible to start a process from an eclipse plugin and provide it with a set environment variables that are set in the plugin ui?
  • Is it necessary to use ProcessBuilder or Runtime.exec() or is there some java-only solution?
  • Is the only way to find an answer to search through the eclipse souce code?
tyler
  • 151
  • 1
  • 2
  • 10
  • Are you using system environment variables? Those tend to be difficult to change on the fly, can you instead refactor the java code to simply take input arguments? Either just do a method call or call the main class with the arguments. – Fredrik Sep 22 '14 at 11:58
  • Yes, the wrapped code uses system environment variables. In fact what i am trying to do is not to change these variables that are defined and exported, but set own ones in my eclipse plugin and kind of 'export' them and then start some process from the plugin that executes the wrapped code, which is then to use the variables exported at runtime from the plugin. – tyler Sep 22 '14 at 12:12
  • @Fredrik refactoring unfortunatelly is not an option, since the wrapped code is used somewhere else 'as is' and my plugin is only a tester for it – tyler Sep 22 '14 at 12:13
  • Right, I think that the java process read the env vars once when starting up, and never rereads them, causing issues when changing vars. I've had this issue several times. Look at http://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w and see if you can force a reread on the env vars before starting the java process. Or perhaps send the vars as part of the java process if thats possible. Edit: Yes, Stehphen C's answer is spot on. – Fredrik Sep 22 '14 at 12:24
  • i will take a look at it, thank you for the hint. – tyler Sep 23 '14 at 06:03

1 Answers1

1

How is it possible to start a process from an eclipse plugin and provide it with a set environment variables that are set in the plugin ui?

  1. Get the env variable names and values from your plugin UI.

  2. Use ProcessBuilder or Runtime.exec() to launch a new (external) process with the appropriate environment variables.

(The 2nd step is the same as what you would do if you weren't using Eclipse.)

There may also be an Eclipse-specific way of doing this, but underneath the hood that will have to use ProcessBuilder or Runtime.exec().

Is it necessary to use ProcessBuilder or Runtime.exec() or is there some java-only solution?

If you want to run the Java code as a separate process, then at some level you have to use ProcessBuilder or Runtime.exec(). Conversely, while it is possible to run a Java application in "the current JVM", you won't be able to change the environment variable settings for the current application. (The Map you get from System.getEnv() is documented as "not modifiable".)

(I'm not sure what you mean by "java only". You could argue that one JVM launching another using ProcessBuilder or Runtime.exec() is "java only".)

Is the only way to find an answer to search through the eclipse souce code?

No. You could also find the answer by reading the javadocs ... like I just did :-)

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • 'There may also be an Eclipse-specific way of doing this ...' -> this is what i am looking for. by 'java only' i meant: all the code that will be executed is packaged in one plugin, using ProcessBuilder or Runtime.exec it seems to be best to put the code-to-be-executed somewhere and call it from the plugin – tyler Sep 22 '14 at 12:50
  • @tyler - I have explained that running within the same JVM (e.g. Eclipse) won't work. It won't let you change the environment variables. Hence, unless you want some Eclipse-specific *functionality* (e.g. the child process integration with the Eclipse / plugin UI) there is little point in looking past `ProcessBuilder` and friends. – Stephen C Sep 22 '14 at 13:35