How do you install JDK? - eclipse

I have eclipse and I can test run java apps but I am not sure how to compile them. I read that I should type javac -version into my cmd.exe and see if it is recognized. It is not. So I went to sun's website and downloaded/installed JDK v6. Yet it still says 'javac' is an unrecognized command. What am I doing wrong?
Thanks!
UPDATE
OK after reading some replies it seems like what I am trying to do is create a .jar file that can be ran on another computer (with the runtime). However I am having trouble figuring out how to do that. This might be because I am using Flex Builder(eclipse), but I added the ability to create java projects as well.
Thanks
UPDATE
OK I do not want to make a JAR file, I am not trying to archive it...the whole point of making a program is to send it to users so they can use the program...THAT is what I am trying to do...why is this so hard?

To setup Eclipse to use the JDK you must follow these steps.
1.Download the JDK
First you have to download the JDK from Suns site. (Make sure you download one of them that has the JDK)
2.Install JDK
Install it and it will save some files to your hard drive.
On a Windows machine this could be in c:\program files\java\jdk(version number)
3.Eclipse Preferences
Go to the Eclipse Preferences -> Java -> Installed JREs
4.Add the JDK
Click Add JRE and you only need to located the Home Directory. Click Browse... and go to where the JDK is installed on your system. The other fields will be populated for you after you locate the home directory.
5.You're done
Click Okay. If you want that JDK to be the default then put a Check Mark next to it in the Installed JRE's list.

You don't need a separate compiler, eclipse already compiles the application for you. What you probably want to do is to create an "executable" JAR file, which you can do in eclipse by selecting File->Export->Runnable JAR file.
Note, however, that the resulting JAR file is not a "real" (i.e. Windows binary) executable - it still needs a JRE installed on the target computer. There isn't really a way to create windows binaries; that's not how Java works. On the upside, it will work without recompilation on a Linux or MacOS machine (if it has a JRE installed).

javac is located in the "bin" folder of your JDK installation. In order to run it you must either use full path or add this directory to your systems search path via the Control Panel.
If you installed to c:\program files\java\jdk1.6.0 your call will have to look like this:
c:\> "c:\program files\java\jdk1.6.0\bin\javac" -version

Umm, eclipse is an IDE, it compiles things as you go. You don't need javac.

If you have Eclipse installed and you can write new java apps from within it, your compilation should work already..
Eclipse automatically builds/compiles your system when you're saving new Java files. Just try to write a new simple Hello world app, printing something to the console (just type sysout and Ctrl+Space inside Eclipse)

Eclipse automatically compiles all project in the workspace. YOu can disable this option if you like under Project->Build Automatically.

A JAR file can function as an executable, when you export your project as a JAR file in Eclipse (as Michael Borgwardt pointed out) you can specify what's the executable class, that meaning which one has the entry point [aka public static void main(String[] args)]
If the user installed the JRE he/she can double-click it and the application would be executed.
EDIT: For a detailed explanation of how this works, see the "How do I create executable Java program?"

Eclipse to use the JDK you must follow these steps.
1.Download the JDK
First you have to download the JDK from oracle site.
Download link - > https://www.oracle.com/technetwork/es/java/javasebusiness/downloads/index.html
2.Install JDK
Install it and it will save some files to your hard drive. On a Windows machine this could be in c:\program files\java\jdk(version number)
3.Eclipse Preferences
Go to the Eclipse Preferences -> Java -> Installed JREs
4.Add the JDK
Click Add JRE and you only need to located the Home Directory. Click Browse... and go to where the JDK is installed on your system. The other fields will be populated for you after you locate the home directory.
5.You're done
Click Ok. If you want that JDK to be the default then put a Check Mark next to it in the Installed JRE's list.

Related

Configuring Eclipse for using with MSYS2

My I use Eclipse Mars and MSYS2. Eclipse does not recognize my MSYS2 installation. It contains Mingw-w64 for 32 bit compilation. Things I have found on the Internet did not work. What should I do?
Well, a bit late to the party, but it looks like there is nothing special about MSYS2: the usual procedure of setting up Eclipse to work with MinGW-w64 installation worked for me just fine.
The main issue is that as described in Eclipse CDT FAQ to detect MinGW toolchain CDT tries to find mingw32-gcc.exe in PATH, while MinGW-w64 have only gcc and i686-w64-mingw32-gcc.exe in its bin directory, so just adding MinGW-w64 bin directory to PATH won't work:
Despite having g++.exe or gcc.exe on your PATH and having defined
MINGW_HOME, you may still get a “Toolchain "MinGW GCC" is not
detected” message (CDT 8.4 on Luna 4.4.0). Make sure that a file
called "mingw32-gcc.exe" exists in MINGW_HOME\bin.
As it mentioned further in Eclipse CDT FAQ, the easy fix would be to copy i686-w64-mingw32-gcc.exe to mingw32-gcc.exe:
If it doesn't exist
(which happens with MinGW-W64), copy a -gcc.exe file (e.g.
i686-w64-mingw32-gcc.exe) to mingw32-gcc.exe. If the dreaded message
still lingers around, reboot your system (don't just logout and
login).
After you did the copying described above, you can add MinGW-w64 bin directory to PATH and Eclipse will recognize the MinGW-w64 installation as MinGW toolchain. Since I don't like idea of polluting neither system nor user environment variables in Windows settings, I usually write a short batch file which adds MinGW-w64 bin directory to PATH and then starts Eclipse for me:
SET PATH=c:\msys32\mingw32\bin;%PATH%
START c:\eclipse\eclipse.exe
Even more later to the party - for me worked this solution: Right click project -> Properties -> C/C++ Build -> Environment: set environment variable MINGW_HOME to C:/.../msys64/mingw64 click OK, and rest is automatically done by eclipse

Error Creating a new Glassfish 4 server with Oracle Tools for Kepler

I installed Oracle Tools for Kepler with no problem. But when I want to create a new server and fill in the directory to the server (which is valid, C:/glassfish4/glassfish), nothing happens. I can only click 'back' or 'cancel'.
attached image for better understanding
Thanks in advance
http://es.tinypic.com/view.php?pic=x3tkea&s=8#.U2jz6vl5NUU
http://es.tinypic.com/view.php?pic=2n0ldtu&s=8#.U2j1_Pl5NUU
This is really a weird error/bug/whatever. I have exactly the same setup on my home workstation and it works if I choose jdk1.7.0_55 or Default JRE as JDK in the dialog. It doesn't work when I choose jre7.
You can try to select Default JRE and see if this works.
Another option is to try a clean start of Eclipse. Edit the eclipse.ini file located in your Eclipse install directory and insert -clean as first line.
If you have downloaded a multi-language version of GlassFish 4, the problem may be related to that. You then may try it with the english only version.
If this also doesn't help you can also add the server runtime manually in the folder <WORKSPACE>\.metadata\.plugins\org.eclipse.core.runtime\.settings\ in a file named org.eclipse.wst.server.core.prefs (if it doesn't exist, create it). It should look similar to this:
eclipse.preferences.version=1
module-start-timeout=300000
runtimes=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>\r\n<runtimes>\r\n <runtime id\="GlassFish 4.0" location\="C\:/glassfish4/glassfish" name\="GlassFish 4.0" runtime-type-id\="org.glassfish.jst.server.runtime.glassfish4" server_definition_id\="org.glassfish.jst.server.runtime.glassfish4" timestamp\="0" vm-install-id\="1399586723676" vm-install-type-id\="org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType">\r\n <map key\="generic_server_instance_properties" sunappserver.rootdirectory\="C\:/glassfish4/glassfish" sunappserver.runtimeName\="GlassFish 4.0"/>\r\n </runtime>\r\n</runtimes>\r\n
(The format looks a bit weird with the \r\n but this should be correct.)
You have to change the vm-install-id in this configuration, look for the ID of your JDK in the file <WORKSPACE>\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.launching.prefs. There should be an entry with your JDK. Copy the vm-install-id from this entry.
Make sure to have the right path to your GlassFish installation in that file.
After saving the file, start Eclipse and try to create a new server. The runtime should then be available when you choose GlassFish 4.0.
See also:
How to run eclipse in clean mode? and what happens if we do so?

JDK and Eclipse not working properly

Okay, so i wiped my PC clean today. Upon attempting to install Eclipse and the JDK i hit a snag.
I install the "ADT Bundle" from android developers.
I install the JDK to the default path
When i try to open Eclipse, it cannot find my JRE (which from what i understand comes in the JDK which i downloaded from the oracle website). I know it is properly installed because i can navigate to the install path and run java programs using the exe.
iv heard something about modifying environment variables to get it to find it but nothing has worked... i dont have a "PATH" variable listed.... i do have a "Path"... but changing either one does nothing to fix the problem... I have done this installation many times and it SHOULD be working...
any help on the subject would be greatly appreciated.
Since you wiped it clean, have you tried downloading Java Runtime Environment again (maybe you wiped it too)
The solution to this problem is such:
The "Path" enviroment variable needs to be under -user variables- NOT system variables
make a new user variable named "Path" and set the value to the absolute path of the java compiler
for me it was something like C:\Program File\Java\JDK1.7\bin
that should fix the problem of eclipse not finding the JDK/JRE...
if it complains about "JNI" then you need to make sure you are running the JDK and eclipse as the same achitechture (64bit java wont work with 32bit eclipse)
The "Path" environment variable needs to be defined or updated. If you are working in Windows 8 then you can navigate by following these steps:
Open > File Explorer
Right Click "Computer"
Select "Properties"
On the left hand side select "advanced system settings"
Under the "Advanced" tab, select "Environment Variables"
Locate the Path directory in the list under System
Edit the directory by entering a semi-colon after the last entry and then manually typing the location of your Java directory.
After the full address is entered, (Should look similar to --> C:\Program File\Java\JDK1.7\bin) click "Ok" on the screen to save your changes.
Something to keep in mind while updating your path. Make sure that you direct the path to the compatible version. If you downloaded the 64 bit eclipse, choose the 64 bit Java. If you do not remember the Eclipse version you downloaded, check your download file name against the files offered on Eclipse's website. If you follow these steps Eclipse should open up without a problem.

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

Ant "JAVA_HOME does not point to the JDK" - but it does

I cannot run my Ant build.xml since I updated to java 1.7.0_52 (or there about). I have been running it for years through Eclipse locally on my Windows 7 laptop - but with this latest jave jdk update somethings different (?).
BUILD FAILED
C:\workspace\WaterAspectsModel3\build.xml:329: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "D:\Morten\Java\jdk1.7.0_52"
This is my jdk! So JAVA_HOME is pointing to a jdk (as is also clear from the error message). I have my JAVA_HOME with bin folder in my path as always. I've configured Eclipse - external tools configuration - with a JRE pointing to the same D:\Morten\Java\jdk1.7.0_52 (through "Separate JRE" setting). And I've tried a number of other configurations - all without luck.
I've been reading the tonnes of answers on lists here and other places and think I've tried all suggestions. Most seem to be cases where JAVA_HOME actually points to a jre and just needs to be corrected to jdk or there's a ";" in the path or something like that. None of these seem to apply in my case.
Any suggestions?
I was having the same message when running ANT through Eclipse.
What worked for me:
In Eclipse, access the menu: "Window -> Preferences";
Access "Ant -> Runtime", at the tree;
Access the Classpath tab;
Expand the "Global Entries" item;
Inside Global Entries, the path to tools.jar was wrong. It was pointing to an older version of Java;
I removed the wrong entry and added the correct one with "Add External Jars" button. It worked.
The solution
"Had to copy C:\Program Files\Java\jdk1.6.0_10\lib\tools.jar to C:\Program Files\Java\jre6\lib\ext" from here was great helpful and worked for me.
The real message is that Ant can't find com.sun.tools.javac.Main.
Which, together with the fact that the latest "Sun" (Oracle) JDK is 1.7.0_13 (or maybe _14, but definitely not the "_52" that your install dir indicates), makes me think that you're not using a distribution that Ant recognizes. You can verify this by running jar tvf $JAVA_HOME/lib/tools.jar, and looking for that class.
The Ant Manual talks about ways to work with different compilers. Since I've only used "Sun" compilers, I'm afraid that I can't give you any pointers.
Edit: you could also try setting fork="yes" in your build file. This should run the compiler executable rather than trying to invoke the compiler class.
Try adding JAVA_HOME\bin to your env PATH .
Add JAVA_HOME to Eclipse-Preferences-Java-Build Path-Classpath Variables.
Sorry for the unearthing,
I had the same problem, my solution doesn't need any modification in environment variables, and works for a recent JDK where the tools.jar can't be found.
Just go in the small down-arrow next to the ant run button
Then click "Configure external tools"
Then in JRE tab
Select a JDK installed on your computer, not a JRE, no problems if the selected JDK is not the same JDK/JRE as in your project.
It should be work now.