start code from eclipse plugin with own environment variables - java

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?

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?
Get the env variable names and values from your plugin UI.
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 :-)

Related

IntelliJ IDEA does not see my environment variables

I've configured an environment variable in ~/.bashrc as well as ~/.profile.
When I run my application via IDEA's Gradle Configuration, my environment variable is apparently not available. E.g. I am referencing this variable within application.yml, but the default value is being used instead. Running Gradle from the command line correctly picks up my variable.
How can I configure IDEA to load this environment variable in a global way, so I don't have to manually add it to every project where I need it (~20 projects)?
Note: running on Manjaro Linux v18.
It turns out IDEA will pick up environment variables if you define them either in e.g. /etc/environment or /etc/profileor if you use ZSH,~/.zshrc`.
After setting variables, you to execute source ~/.bashrc and source profile to update and execute the file contents.
# josh-m I ran into the same problem in my IntelliJ IDEA setup with Maven.
In the end, I continued to keep the settings on each project, as there were not many, but when I studied the problem I found this IntelliJ support link:
https://youtrack.jetbrains.com/issue/IDEA-141832
At least at that time, this functionality was not available in IntelliJ directly, although the following plugin, mentioned in the comments of the support article, could perhaps be of some help:
https://plugins.jetbrains.com/plugin/7861-envfile/
launchctl setenv HADOOP_VERSION 3.1.1.7.2.8.0-SNAPSHOT
Execute in this way to set your environment variables in MacOS for them to be used by other applications.
After that restart your IDE.

How run a java jar with a customize name or id in the cmd? [duplicate]

Our Java Swing application is running on Windows and wrapped in an exe file using Launch4J.
We would like to customize our application process name and description (in Windows' task manager) as it is currently "javaw.exe" and "Java Platform SE binary" (which is confusing for our customers).
While older versions of Launch4J enabled to change this using <customProcName>, this option is now defunct as it is not working anymore as of Win7.
Is there any other (simple) workaround to customize our application process name and description?
For instance, changing javaw.exe executable filename seems like an approach (as we embed it in the wrapped exe file) but how then indicate to launch4j that the jvm file name changed?
Another option could be to create a launcher exe file: maybe an overkill?
Any ideas / hints are more than welcome; thanks!
If you're looking to roll your own solution, you're going to want to look at JNI and the Invocation API in particular JNI_CreateJavaVM() which is used to create a VM, find the main method GetStaticMethodID() and invoke it with CallStaticVoidMethod.
This is what the java.exe, javaw.exe and a variety of other native launchers do internally. Some examples include:
OpenJDK java.c
IntelliJ WinLauncher.cpp
WinRun4J VM.cpp
If you want don't want to integrate a native build system with your java build system, an approach is to build a static launcher.exe in advance, and treat it as a static binary blob. Then during your java build, modify the binary blob using java, to update the VERSIONINFO, icon and splash screen. An example of this approach can be seen in IntelliJ LauncherGeneratorMain.java
If you can afford it, JSmooth seems to do what you need. It's last released in 2007 though. A note about its license taken from the app itself:
The executable generated (the launchers created by JSmooth) are under the LGPL with a "runtime exception" similar to the gcc licence exception: It is not required that you distribute the source code with it, nor that you publish a notice mentionning jsmooth.
When using JSmooth, there is a section labeled "Skeleton" that allows you to select some pre-defined parameters. One of them is a "Windowed Wrapper" that is fit for GUI applications described as follows:
This skeleton wraps GUI applications.
No console I/O is displayed
If no Java VM is found, it is able to display a configurable URL (typically to a java download page).
Arguments can be passed to the application (either use the JSmooth default argument mechanism, or create a shortcut with arguments).
The important thing in this skeleton is to check the option "Launch java app in the exe process" which results in running the JVM in the same process as the wrapper exe. This means only the exe is shown in the Windows Task Manager, as opposed to both the exe and the java process.
The alternative is to write your own wrapper. See this Oracle guide for how to invoke the JVM from a native application.
I did similar things with WinRun4J, as far as I remember it can be used commercially because it is CPL licensed. Checked this today: settings the process name still works (initially did this on XP) with Windows 7.
According to the website you simply create a ini file which tells WinRun4J what to run:
main.class=org.something.MyMainClass
classpath.1=*.jar
(there are many more parameters, you can set where the JRE can be found and more)
In a second step you copy the winrun4j.exe to something that fits your application:
copy winrun4j.exe yourapplication.exe
(There is a version for Windows x64 too)
Then you have RCEDIT (comes with WinRun4j) add the ini to the exe:
rcedit /N yourapplication.exe yourapplication.ini
This seems pretty old, and not really worth the effort, but it may also be what you are looking for: Java exe Maker.

Why do I see Eclipse as 'javaw.exe' in Windows task manager?

Sorry if this is too naive, but shouldn't it be something like eclipse.exe?
The links provided in the comments are quite informative, but if you still aren't able to understand why Eclipse shows as javaw.exe in the Task Manager, then refer to this answer. (Excerpt below)
javaw: (java windowed) Application executor not associated with console. So no display of output/errors. Can be used to silently push the output/errors to text files. Mostly used to launch GUI based applications.
Eclipse is a GUI based development tool and hence the process associated with Eclipse reads javaw.exe in Task Manager.
If you need it to be run as eclipse.exe, you can pass in the following parameter in your eclipse configuration file:
-vm <your java installation folder>\bin\client\jvm.dll
Eclipse is a program, which runs upon Java Virtual Machine. javaw.exe is a process, corresponding to JVM.

How can I retrieve environment variables(Jenkins) in java

I'm setting some environment variables by some windows batch commands in the pre-build steps for a Jenkins build. I need to use these variables for a few java selenium test cases which will run during as part of the build.
set HD_KF_TC_IN_LOOK_UP_KET_FEATURE_XLS = %WORKSPACE%\selenium_input_files\Key_Features\en_US
I have tried to retrieve these environment variable values by using System.getEnv(HD_KF_TC_IN_LOOK_UP_KET_FEATURE_XLS) commands. But this is not working as the values for these variables is always NULL.
Please let me know where I'm I going wrong?
As indicated in my comment, I do not completely understand your setup. In general, however, environment variables set in one Jenkins build step do not propagate outside of it and can't be accessed from other build steps. You need to use EnvInject plugin to achieve that.

Environment variables in Eclipse

I am able to run a sample hadoop program from the command prompt and am trying to run the same program from Eclipse, so that I can debug it and understand it better.
For the command line program, some environment variables are set in the .bashrc and the same are being read as System.getenv().get("HADOOP_MAPRED_HOME") in the hadoop program. But, when I am running a java program with System.getenv().get("HADOOP_MAPRED_HOME"), from Eclipse I am getting null.
I tried passing -DHADOOP_MAPRED_HOME=test to VM parameters in the runtime configurations from Eclipse, but still getting null in the standalone program. How to make the environment variables visible within Eclipse? When I iterate through System.getenv() in Eclipse, I see lot of variables like DISPLAY, USER, HOME and others. Where are they set? I am using Ubuntu 11.04.
You can also define an environment variable that is visible only within Eclipse.
Go to Run -> Run Configurations... and Select tab "Environment".
There you can add several environment variables that will be specific to your application.
I've created an eclipse plugin for this, because I had the same problem.
Feel free to download it and contribute to it.
It's still in early development, but it does its job already for me.
https://github.com/JorisAerts/Eclipse-Environment-Variables
The .bashrc file is used for setting variables used by interactive login shells. If you want those environment variables available in Eclipse you need to put them in /etc/environment.
You can set the Hadoop home directory by sending a -Dhadoop.home.dir to the VM. To send this parameters to all your application that you execute inside eclipse, you can set them in Window->Preferences->Java->Installed JREs-> (select your JRE installation) -> Edit.. -> (set the value in the "Default VM arguments:" textbox). You can replace ${HADOOP_HOME} with the path to your Hadoop installation.
You can also start eclipse within a shell.
You export the enronment, before calling eclipse.
Example :
#!/bin/bash
export MY_VAR="ADCA"
export PATH="/home/lala/bin;$PATH"
$ECLIPSE_HOME/eclipse -data $YOUR_WORK_SPACE_PATH
Then you can have multiple instances on eclipse with their own custome environment including workspace.
I was trying to achieve this but in the context of a MAVEN build. As part of my pom.xml configuration, I had a reference to an environment variable as part of a path to a local JAR:
<dependency>
<groupId>the group id</groupId>
<artifactId>the artifact id</artifactId>
<version>the version</version>
<scope>system</scope>
<systemPath>${env.MY_ENV_VARIABLE}/the_local_jar_archive.jar</systemPath>
</dependency>
To compile my project, I had to define the environment variable as part of the run configuration for the maven build as explained by Max's answer. I was able to launch the maven compilation and the project would compile just fine.
However, as this environment variable involves some dependencies, the default "problems" view of Eclipse (where compilation errors/warnings usually show) would still show errors along the lines of Could not find artifact and systemPath should be an absolute path but is ${env.MY_ENV_VARIABLE}/the_local_jar_archive.jar.
How I fixed it
Go into Window -> Preferences -> General -> Worksapce -> Linked Resources and define a new path variable.
Finally, in my case I just needed to Right click on my pom.xml file, select Maven -> Update Project and the errors disappeared from the "Problems" view.
For the people who want to override the Environment Variable of OS in Eclipse project, refer to #MAX answer too.
It's useful when you have release project end eclipse project at the same machine.
The release project can use the OS Environment Variable for test usage and eclipse project can override it for development usage.
I was able to set the env. variables by sourcing (source command inside the shell (ksh) scirpt) the file that was settign them.
Then I called the .ksh script from the external Tools

Categories

Resources