tags:

views:

1257

answers:

7

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?

+2  A: 

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
Daniel Schneller
+2  A: 

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

skaffman
By default Eclipse uses the JRE not the JDK. You have to add it under the Installed JREs list.
Bernie Perez
You still don't need the JDK, since eclipse has its own compiler
Michael Borgwardt
JRE doesn't include javac.exe; only JDK has that. Michael is correct - Eclipse has its own bundled in.
duffymo
Some eclipse plugins require you to run Eclipse under a JDK, not the JRE. And eclipse doesn't have it's own "javac.exe" bundled, but it's own compiler.
matt b
+1  A: 

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)

Juri
Yes this works when I am in eclipse, how do I compile it or create an executable that can run on another computer..I am new to Java sorry.
John Isaacks
John, you should consider adding this question to your original question, to capture its evolution.
Don Branson
A: 

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

Igor Zelaya
+4  A: 

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).

Michael Borgwardt
+3  A: 

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.

Bernie Perez
A JRE is enough for that as Eclipse has its own (jikes?) compiler built in.
Daniel Schneller
Well the question asked how to install a JDK and he is running Eclipse. Its nice to have the JDK since it has extra stuff and the source.
Bernie Perez
A: 

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?"

EdSG
Thanks, the JAR file wasn't running, but then I set the entry point class and now it works..thanks!
John Isaacks