Running ant through eclipse it doesn't find environment variables, but running ant through terminal is fine - eclipse

When i run ant through Eclipse some targets build fine, but ones that need a specific environment variable i.e. SOME_SDK that i set in my .profile file won't work. Even if i try to echo out ${env.JAVA_HOME}, Ant through eclipse will just print out that string. But if i run ant through the terminal (i.e. ant sometarget), it will find JAVA_HOME and SOME_SDK and echo those paths out. Ive checked eclipse that it's pointing to the same ant that is in my environment path (/usr/share/java/ant-1.8.1). Any ideas?
Thanks

When you open a terminal, the environment variables in your .profile get loaded. When you start Eclipse via the windowing system it is not being run under your user account, so your .profile is not available to Eclipse at runtime.
At least that's the case on my linux machine and I'm guessing its the same on a Mac. I put the environment variables that Eclipse needs to see in the /etc/profile file, which gets loaded at system startup.

You can specify variables in your Ant run configuration (Run As > Ant Build... > Environment tab).

I'm thinking that the Eclipse IDE environment handling is a bit spoiled, at least in *nix environments. Although similar to Kevin's case, what I'm finding is that although I do create a separate run profile and -successfully - run the test/install goals (maven instead of ant here, but that's negligible noise), and specify the JAVA_HOME environment variable for that run profile, the project does not validate the POM file.
All I'm left with is two separate choices:
use the /etc/profile approach, or
Edit eclipse.ini file including the system property there.
I think the latter is a cleaner workaround since it affects a configuration file that is only for the problematic application after all. Nevertheless, Eclipse should use the variables found in the user's path and leave us at ease to produce :)

What you need to do is remove the java.exe from c:\Windows\System32.
The reason is, in your %PATH% environment variable, it includes c:\Windows\System32, which has a java.exe file. If you run c:> java -version from a command prompt, you'll see that the version you're running does not match the jdk version.
Once you've done this, you will need to add: %JAVA_HOME%/bin to the %PATH% environment variable, so you can execute the java.exe from the jdk when running apps. Be sure to restart any application (including the command prompt) before expecting it to pick up the change.

Add this line to ~/.bashrc (or you can use another profile file):
alias start-eclipse='open /Applications/eclipse/Eclipse.app'
(Don't forget to run source ~/.bashrc)
If you run start-eclipse from the command line, all environment variables will be picked up. This way, you only need to maintain a single set of environment variables across both command-line and eclipse environments.
NOTE: Stolen from Chris Fegley's answer at Launch mac eclipse with environment variables set

Related

Installing Maven 3.1.1 on Maverick and working with eclipse-Kepler TLM terminal plugin

I recently installed OS X 10.9 Mavericks. As it does not come with Maven pre installed, I installed Maven 3.1.1 manually. I exported maven and set its path in .bash_profile. It is accessible from command line. But it is not accessible from within eclipse TLM terminal plugin or Wicked Shell plugin.
One workaround I got is, I added same export statement in .bashrc and it is accessible now from within eclipse aforementioned terminal plugins. Is there any better solution for it? Can anybody explain how many .bash* files are there in mac and when each one is initialised ?
In order to have eclipse pick up the new addition to your PATH, you need to edit /etc/launchd.conf to set the environment variable explicitly. Environment variable substitutions such as $PATH will not work. To correct do an echo $PATH in terminal and copy what it returns. Then, add this line to the launchd.conf file and restart your machine.
setenv PATH <your echo $PATH results here>
After, all should return to normal in eclipse. Hope this helps!

How to change default JRE for all Eclipse workspaces?

I have one JRE in C:\Program Files (x86)\Java\jre6 and that was the only one at the time I installed Eclipse. I have subsequently installed a complete JDK in C:\home\SFTWR\jdk1.6.0_21 and changed my JAVA_HOME environment variable to that. However, every time I start a new Eclipse workspace, it only picks up the old JRE and I have to manually remove it and add the new one.
How do I bind my Eclipse install to the new JDK so that every new workspace points to that only? I checked eclipse.ini but there was no reference there to which JRE to go to.
UPDATE:
I went into Prefs->Java->Installed JREs, added the new location, marked it as default, removed the other, and it was effective only for the current workspace. However, when I opened a new workspace, only the old JRE was available. So this didn't change the core Eclipse config that is applicable across all (at least new) workspaces
Finally got it: The way Eclipse picks up the JRE is using the system's PATH.
I did not have C:\home\SFTWR\jdk1.6.0_21\bin in the path at all before and I did have C:\Program Files (x86)\Java\jre6\bin. I had both JRE_HOME and JAVA_HOME set to C:\home\SFTWR\jdk1.6.0_21 but neither of those two mattered. I guess Eclipse did (something to the effect of) where java (or which on UNIX/Linux) to see where Java is in the path and took the JRE to which that java.exe belonged. In my case, despite all the configuration tweaks I had done (including eclipse.ini -vm option as suggested above), it remained stuck to what was in the path.
I removed the old JRE bin from the path, put the new one in, and it works for all workspaces.
The Installed JREs is used for what JREs to execute for your downstream Java projects and servers. As far as what JVM or JRE that is used to execute Eclipse process (workbench) itself that is controlled by your environment, history and eclipse.exe binary. So eclipse.exe itself decides what JRE Eclipse will execute itself with, not installed JREs preferences since those are not read until OSGi framework is up and running which is loaded after the JVM/JRE is picked.
So for new workspaces, Eclipse is going to use its currently executing JRE to populate the JRE prefs.
The best way I know how is to force eclipse.exe to use the JRE that you tell it via the -vm switch. So in your eclipse.ini do this:
-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502
-vm
/path/to/exactly/what/jre/you/want/as/default/javaw.exe
...
Open the Java > Installed JREs preference page. Check the box on the
line for the JRE that you want to assign as the default JRE in your
workbench. If the JRE you want to assign as the default does not
appear in the list, you must add it. Click OK.
Source-http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-assign_default_jre.htm
In eclipse go to
Window-> Java -> Installed JREs
You can remove your current installed jre and add the jdk by specifying the path to where the jdk is installed.
I ran into a similar issue where eclipse was not using my current %JAVA_HOME% that was on the path and was instead using an older version. The documentation points out that if no -vm is specified in the ini file, eclipse will search for a shared library jvm.dll This appears in the registry under the key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment that gets installed when using the windows java installer (key might be a bit different based on 64-bit vs 32-bit, but search for jvm.dll). Because it was finding this shared library on my path before the %JAVA_HOME%/bin, it was using the old version.
Like others have stated, the easiest way to deal with this is to specify the specific vm you want to use in the eclipse.ini file. I'm writing this because I couldn't figure out how it was still using the old version when it wasn't specified anywhere on the path or eclipse.ini file.
See link to doc below:
http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/misc/launcher.html?cp=2_1_3_1
Finding a VM and using the JNI Invocation API
The Eclipse launcher is capable of loading the Java VM in the eclipse process using the Java Native Interface Invocation API. The launcher is still capable of starting the Java VM in a separate process the same as previous version of Eclipse did. Which method is used depends on how the VM was found.
No -vm specified
When no -vm is specified, the launcher looks for a virtual machine first in a jre directory in the root of eclipse and then on the search path. If java is found in either location, then the launcher looks for a jvm shared library (jvm.dll on Windows, libjvm.so on *nix platforms) relative to that java executable.
If a jvm shared library is found the launcher loads it and uses the JNI invocation API to start the vm.
If no jvm shared library is found, the launcher executes the java launcher to start the vm in a new process.
-vm specified on the command line or in eclipse.ini
Eclipse can be started with "-vm " to indicate a virtual machine to use. There are several possibilities for the value of :
directory: is a directory. We look in that directory for:
(1) a java launcher or
(2) the jvm shared library.
If we find the jvm shared library, we use JNI invocation. If we find a launcher, we attempt to find a jvm library in known locations relative to the launcher. If we find one, we use JNI invocation. If no jvm library is found, we exec java in a new process.
java.exe/javaw.exe: is a path to a java launcher. We exec that java launcher to start the vm in a new process.
jvm dll or so: is a path to a jvm shared library. We attempt to load that library and use the JNI Invocation API to start the vm in the current process.
My answer will overlap with amphibient's while adding on to it.
Your JAVA_HOME variable is fine, but you also need to append the following to your Path variable :
;%JAVA_HOME%\bin
This will allow your applications in your Windows environment to access your JDK. You should also restart your computer once you've added these environment variables before checking out if they work. In my case, even logging out and then back in didn't work : I had to completely restart.
If you want to check if the environment variables are set correctly, you can open up a command prompt and type >echo %JAVA_HOME and >echo %Path% to see if those variables are working correctly. While snooping around for solutions, I have also run into people claiming that they need to add quotations marks (") around the environment variables to make them work correctly ("%JAVA_HOME%"\bin) if your JAVA_HOME path includes spaces. I thought that this was my case at first, but after doing a full restart my variables seemed to work correctly without quotation marks despite the spaces.
when you select run configuration, there is a JRE tap up next the main tap, select "Workspace default JRE(JDK1.7)".
Be sure to use the jdk in Prefs->Java->Installed JREs ->Execution Environment
I have faced with the same issue. The resolve:
- Window-->Preferences-->Java-->Installed JREs-->Add...
- Right click on your project-->Build Path-->Configure Build Path-->Add library-->JRE system library-->next-->WorkSpace Default JRE
try to change the order:
right click on you project-> BuildPath->Configure...->Order and Export tab -> move jre7 UP.
I navigated to:
Eclipse>Pref>Java>Installed JRE>Search...
2 of them popped up and I checked the latest one. Before I did this I also went to About>Check for Updates and updated it. I didn't have to reinstall any JRE or JDK either. I might have done it a while back, except it was with 1.6 not 1.4. Hope that helps!
On windows I've tried different approaches
- setting JAVA_HOME, JRE_HOME and extending the PATH to point to the desiered jre18 but nothing helped
- disabling the JRE17 in the java control panel didn't helped either
What helped me out was to force eclipse to use the appropriate JRE in the eclipse.ini file e.g.
-vm
C:\java\jdk1.8.0_111\jre\bin\javaw.exe
#Stefan Michev answer also worked for me,
edit eclispe.ini to point to your installed JRE
-vm
C:\Program Files\Java\jdk-18.0.1.1-openj\bin
but to elaborate;
My application server uses JAVA_HOME and thus PATH so I needed Eclipse to pick up another JRE to run (so to be compatible with an installed plugin).
This Eclipse JRE is different than
than setting Window-> Preferences -> Java -> Installed JREs which as you see in the below image, is for building Java projects, so you can have different projects with different JDKs
Eclipse Installed JREs

Can I use Windows Environment Variables into Eclipse Workspace path?

When you start Eclipse for the first time, it asks you the path for the workspace. Now, normal path should be C:\Users\USERNAME\Workspace but, if I try to set %USERPROFILE%\Workspace it doesn't work, it creates a folder called %USERPROFILE%\Workspace under the Eclipse folder.
So the question is: how to use windows environment variables to specify workspace path? Thank you.
P.S: I know that Eclipse has got customizable environment variables but I didn't try them and I want to use them neither.
You can run eclipse.exe -data %USERPROFILES%\myCustomWorkspaceDir to have the workspace in your wanted sub directory. In this case, you will not even get the dialog asking for the workspace.
There are still more runtime options for Eclipse.

Eclipse is not starting

Eclipse starts from terminal but when i try to start it from gui then it says java runtime environment don't found.
Environment variables are:
JAVA_HOME="/opt/jdk1.6.0_20"
PATH=$PATH:"/opt/jdk1.6.0_20/bin"
And one more thing so far i normally copy jre folder from jdk to eclipse directory, which works fine but what is actual way. i mean this won't be standard way to do that.
Thanks in advance
And one more thing so far i normally
copy jre folder from jdk to eclipse
directory, which works fine
No, this is not necessary or desirable. You should not have to do any such thing.
If you open up a command shell and type "java -version", what comes back?
Edit eclipse.ini (from the Eclipse folder) and add the following at the beginning:
-vm
C:/Java/jdk1.6.0_22/bin
where C:/Java/jdk1.6.0_22/bin should be your path to the java bin directory.
Keep it on two lines.
Well the reason eclipse won't start from GUI despite of the fact that environment variables are set, is:
You have set the variable in bashrc or bash_profile, these two are for the specific logged in user, but you also need to set in etc/profile which is for all the users.
So once you set the PATH and JAVA_HOME in etc/profile it will launch from GUI.
Worked for me! ;)
Faced the same problem and the suggestion addressed my problem!
After setting /etc/profile as indicated in the link below, I logged out and logged back in for GUI based Eclipse to work.
http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/

Eclipse just does not start up

I recently uninstalled and then reinstalled Java on my machine and ever since, Eclipse just does not start up. I find this weird, because I uninstalled a different version of Java from the one that I use. I modified the eclipse.ini file, by adding the following:
-vm
C:\Program Files (x86)\Java\jre1.5.0_15\bin\javaw.exe
This made no difference. I then tried to reset the JAVA_HOME variable and I tried both these things again, after reinstalling (well, unzipping) Eclipse again. None of this made any difference, which is why I'm posting this question. Can someone help me out here?
You need to make sure that the path to javaw.exe is in %PATH% environment variable. Basically:
%PATH% = %PATH%;c:\path\to\jre\bin
You can configure this in the environment variables configuration and you can verify this by opening a command prompt and test if java is recognized as a command from inside an arbitrary path.
The %JAVA_HOME% should by the way point to the JDK's /bin folder, not the one of the JRE. Regardless, this environment variable is ignored by Eclipse since it comes with its own compiler.
I uninstalled all the versions of Java I had installed and installed them again. I also deleted Eclipse and downloaded that again. Following this, I edited the eclipse.ini file to include the path the javaw.exe, as I mentioned in the question. Everything works fine now.