How do I run java?
I'm fed up with not being able to use java from home so I'm currently trying to get it working for the command prompt.
java -version works just fine (1.5.0_06 btw).
However when it comes to javac I get - 'javac' is not recognised as an internal or external command, operable program or batch file.
I've heard this is something to do with the path not being set right however how on earth do I go about correcting this path?
I have searched the forum and found similar questions however they always seem to fizzle out before giving me a answer of just what to do.
What would I have to edit and what to?
I don't want to just go in and change anything in case I edit something important....
[736 byte] By [
josquiusa] at [2007-10-2 20:13:03]

This is not a Java specific issue, this is about how your OS finds executables. If it cannot find any matching command in the current directory, it will try to find it in any of the directories specified in the PATH environment variable.
Actually first of all, do you have javac; in other words do you have a JDK or just a JRE?
You have two choices:
- You run javac from a batch file (or better, an ant script), and in this batch file you specify the path to the javac.exe you want (it should be in the same folder as java.exe).
- You set your system's path variable to include your JDK's bin folder (the one where java and javac are located). To do this you open a DOS console and use the set command, about which google can tell you a load more if you need it.
Lokoa at 2007-7-13 22:55:05 >

It appears I don't have JDK (I thought you just needed the one) however I have had it in the past and for some reason it won't reinstall...Gets as far as saying it will now be installed then just vanishes.
In one sentence: a JRE runs Java applications while a JDK allows you to develop them (compile, for one); a JDK includes a JRE so this is what you want.
You can download the latest offical release here:
http://java.sun.com/j2se/1.5.0/download.jsp
Select "Download JDK 5.0 Update 6".
Read the docs that come with it.
Specifically, read the documentation for java.exe and javac.exe here:
http://java.sun.com/j2se/1.5.0/docs/tooldocs/index.html
Good luck and feel free to ask back if you run into a concrete problem.
Lokoa at 2007-7-13 22:55:05 >

This site has quite horrendous documentation which even makes the creation of hello world programs sound complicated.
That update jdk seems to have installed where the netbeans one wouldn't.
Still trying to figure out how to set this path now.
http://img66.imageshack.us/img66/8906/jav4rc.png
is as far as I can seem to get.
Am I pointing at the right thing? Just what am I doing wrong?
bat should be binrather than post links to screenshots, copy and paste the text into the forum. And place it in code tags. http://java.sun.com/docs/books/tutorial/getStarted/index.html
mlka at 2007-7-13 22:55:05 >

> This site has quite horrendous documentation which
> even makes the creation of hello world programs sound
> complicated.
That is simply not true, and although understandable, venting your frustration this way is not going to help you.
> That update jdk seems to have installed where the
> netbeans one wouldn't.
> Still trying to figure out how to set this path now.
>
> http://img66.imageshack.us/img66/8906/jav4rc.png
> is as far as I can seem to get.
> Am I pointing at the right thing? Just what am I
> doing wrong?
I could not access the link you posted (request timed out) so I don't really know anything more.
Assuming you downloaded the JDK, locate it on your hard disk. Find javac.exe. Now that you have the path to this exe, you have to tell Windows how to find it from the directory you want to use it from (again this is NOT a Java specific issue).
If you are going for setting the PATH environment variable, google will bury you under sites explaining how to. Also typing set /? shows you how to use this command (and actually a lot more you do not need to know).
//in DOS console:
set PATH //shows you the current value of this variable
set PATH= [FOLDER WHERE JAVAC.EXE IS];%path% //prepends the right folder to the existing value
set PATH //verify that the javac folder is now in there
This will prepends the right folder to the existing values on the path.
Good luck & calmly read the documentation and try things step by step. You'll get there.
Lokoa at 2007-7-13 22:55:05 >

I am unable to just copy and paste what is written as it is in a dos prompt. I will just copy out little things but for anything big...
Hurrah, I'm getting a different error now (hey its progress)
when I try to javac a hello world made in notepad and saved as jav.java I get:
error: error reading c:\program files\java\jdk1.5.0_06\bin\javac.exe; error in opening zip file
1 error
Any idea?
I didn't know any zips were involved here.
Also is there anyway to save the path? It seems to revert to the version before I added something whenever I load up the prompt again.
> I am unable to just copy and paste what is written as
> it is in a dos prompt.>
FYI, click the icon in the left top corner of your DOS console window, select Edit and you can copy and paste as much as you like.
> when I try to javac a hello world made in notepad and
> saved as jav.java I get:
Probably not causing your current error, but a Java source file must have the exact same name as the public class inside it. Meaning if your main class is called HelloWorld it has to be in a file HelloWorld.java. BTW, Java is case sensitive.
> > error: error reading c:\program
> files\java\jdk1.5.0_06\bin\javac.exe; error in
> opening zip file
> 1 error
>
> Any idea?
> I didn't know any zips were involved here.
Neither did I. I've never seen this exact error.
I'm willing to try and help you on, but you must understand it's extremely hard to guess what could be wrong without more info. If you could post the exact and full command line you are trying, along with the location of your java source file, and the exact and full error message (as you already did post), people will be able to help.
In the meantime, if you just type javac in the console, do you at least get an output like this (and did you read and take this info into account)?
>javac
Usage: javac <options> <source files>
where possible options include:
...
Lokoa at 2007-7-13 22:55:05 >

ah....seems I'm getting nothing now.
As mentioned it wasn't saving my set path so I've had to put it in again but I must have forgot what I put to get it working as I can't seem to get it working....On with...
I have a lot of possible alternatives currently in the list none of which are being found:
F:\>set path
Path= c:\program files\java\jdk1.5.0_06\bin\javac.exe ; c:\program files\java\j
dk1.5.0_06\bin\javac.exe ; c:\program files\java\jdk1.5.0_06\bin\javac.exe; c:\p
rogram files\java\jdk1.5.0_06\bin; c:\program files\java\jdk1.5.0_06\bin; c:\pro
gram files\java\jdk1.5.0_06\bin ;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\Syste
m32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\QuickTime\QTSy
stem\;C:\Sun\AppServer\bin;P?#8597;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
F:\>javac
'javac' is not recognized as an internal or external command,
operable program or batch file.
F:\>
Remove every mention of 'javac.exe' from that PATH list. The PATH liststores the *directories* where executable files can be found. It doesn'tstore the executable name itself.kind regards,Jos
JosAHa at 2007-7-13 22:55:05 >

Just leave your console open for now. We will get to persisting the path later, when the compiling works. One thing at a time. At this point, it's a good thing you're not persisting it since that way you can't do anything wrong.
EDIT: saw the rest of your post now... as JosAH said, and I explictly told you before, you put the FOLDER where javac is located in, there.
Message was edited by:
Loko
Lokoa at 2007-7-13 22:55:05 >

Thought so, its just when it wasn't working I tried other alternatives.
For bog standard stuff right now I have:
F:\>set path
Path= c:\program files\java\jdk1.5.0_06\bin ; C:\WINDOWS\system32;C:\WINDOWS;C:\
WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\Q
uickTime\QTSystem\;C:\Sun\AppServer\bin;P?#8597;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
F:\>javac
'javac' is not recognized as an internal or external command,
operable program or batch file.
F:\>
is set path= c:\program files\java\jdk1.5.0_06\bin ; %path%
The correct way of doing this?
Sorry for this all being a big thing for just doing something that should be simple. I just really have very little experience when it comes to dos.
Okay the folder is in there in some form, yet javac is still not found. That means there's still something wrong with the environment variable. I'm not 100% sure if it is causing the error, but you have spaces in there that should not be there.
//I told you to do this
set path=c:\program files\java\jdk1.5.0_06\bin;%path%
//but you did this instead (look closely at space characters)
set path= c:\program files\java\jdk1.5.0_06\bin ; %path%
As I said I am not 100% sure if this is the problem but getting rid of those spaces is what I would do.
In general, you should try to follow documentation/instructions exactly and literally.
> Sorry for this all being a big thing for just doing something that should be simple. I just really have very little experience when it comes to dos.
That's okay. I'm not giving up yet and there are plenty of other people around here. Google is your friend, by the way.
Lokoa at 2007-7-13 22:55:05 >

I don't know how smar^H^H^Htupid your shell/cmd/set command is,but I would leave out those spaces around the ';' semicolon.kind regards,Jos
I'm gonna be driving home the next hour so he's all yours JosAH ;-)
Lokoa at 2007-7-21 1:45:00 >

> I'm gonna be driving home the next hour so he's all yours JosAH ;-)No need to worry: I'm going to hit him on the head, hard but fair, untilhe's going to like it and then I'll stop ;-)kind regards,Jos
I was googling it all morning with little hope.
Javac works now. Brings up a large list of commands
Hurrah compiling works too. Creates jav.class.
Now to look back and find those old links and how to run this class.
ah, one thing unresolved- how would I save my path?
Message was edited by:
josquius
> Javac works now. Brings up a large list of commands
Good.
> Hurrah compiling works too. Creates jav.class.
> Now to look back and find those old links and how to run this class.
The 'java.exe' command is stored in the same directory as the 'javac.exe'
command. Simply do this (assuming that your current working directory
is where your jav.class is stored):
java -classpath . jav
kind regards,
Jos
> ah, one thing unresolved- how would I save my path?
Open up your control panel, select 'System', 'Advanced', 'Environment Variables'
and add the directory where all the java stuff is stored to the 'path' variable
by Editing or Newing that 'path' variable.
kind regards,
Jos
>>>java -classpath . javWhat should the spacing be in this?Or does that imply I should be putting something else in there and not actually that (probally)
> >>>java -classpath . jav
>
>
> What should the spacing be in this?
> Or does that imply I should be putting something else
> in there and not actually that (probally)
Hehe I'm back :-)
The spacing in this (and in every worthwile sample you read) should be exactly as it is presented to you. The '.' means 'this directory'. If you are running the command from another directory than the one where your jav.class is located, instead of the '.' you put the path to where your .class file is.
Lokoa at 2007-7-21 1:45:00 >

> >>>java -classpath . jav
>
> What should the spacing be in this?
No, that command line reads: start the java.exe (which can be found
through that PATH variable) and use '.' (dot, i.e. the current working
directory as the classpath) and run the 'jav' class. Basiscally it reads"
java [space] -classpath [space] [dot] [space] jav
That '-classpath' thingy is a flag that takes an argument which denotes
the, well, classpath. A 'classpath' is like a 'path', but it only has a meaning
for java: the value is a semicolon separated list of directories where the JRE
should look for classes.
In the example above you only supply one directory: the '.' directory which
is a shorthand for your current working directory.
kind regards,
Jos
I am confused here.I have the class file in F:\ and I have the prompt set to F:\ too.so java-classpath.jav should run it?Or should something go in 'classpath'?
To sort of summarize: there are two separate issues here: path and classpath.
The first is not specific to Java; it's about how your OS (Windows in this case) finds a console command. If you type "javac" or "java" or "myapplication.exe", Windows needs to know where to find this command. This is determined by the environment variable called PATH; every command in every folder specified in here can be run from any folder. Environment variables are system wide settings.
The second issue is specific to Java and is about how the Java runtime finds .class files. This is determined by the CLASSPATH, which can be set as environment variable too, but it is best to specify this as a command line argument to java.exe. Any class which is in any of the folders/jars specified in the CLASSPATH can be found by its fully qualified name by the JRE.
Lokoa at 2007-7-21 1:45:00 >

> I am confused here.
> I have the class file in F:\ and I have the prompt
> set to F:\ too.
> so java-classpath.jav should run it?
No,
java -classpath . jav
should run it. What error do you get?
> Or should something go in 'classpath'?
Something is there; '.' which means 'this directory where you are running from'; in this case "F:\" which is correct since your class file is there as well.
Lokoa at 2007-7-21 1:45:00 >

ah....So I need[code]set classpath = f:\[/cpde]Then java-classpath.jav will run jav.class from F:?
> ah....
> So I need
> [code]
> set classpath = f:\
> [/cpde]
No. Forget about setting the classpath as environment variable. It's not a good idea and I'm sorry I mentioned it. It's best to pass it as a command line argument, which you do with java -classpath
> Then java-classpath.jav will run jav.class from F:?
Is your class file in F:\? If not, you lied before :-)
Is your console at F:\? If not, you lied before :-)
Please type, exactly, as in copy paste it if you must (because in what you write there are again spaces missing), Jos' command line?
What error did you get?
EDIT: Hang in there. We will get this sorted.
Message was edited by:
Loko
Lokoa at 2007-7-21 1:45:00 >

F:\>java-classpath.jav'java-classpath.jav' is not recognized as an internal or external command,operable program or batch file.F:\>
//this is what you typedjava-classpath.jav//this is what you should typejava -classpath . javThere should be 3 spaces.
Lokoa at 2007-7-21 1:45:05 >

> ah....
> So I need
> [code]
> set classpath = f:\
> [/cpde]
> Then java-classpath.jav will run jav.class from F:?
No, reread what Loko wrote in reply #28 and read this:
When you type the name of an executable (such as java of javac), your
operating system consults the PATH environment variable. The value
of this variable is a semicolon separated list of directory/folder names.
It searches every singly directory in that list until it has found something
(or not in which case it'll whine at you).
The CLASSPATH environment is similar but it only applies to the Java
system. On top of that, you don't need to set that variable as an environment
variable, you can also pass it as a command line argument. This might
come in handy when you have different Java applications installed all
over the place. The way to specify that CLASSPATH is as an argument:
java -classpath <your_semicon_separated_list_here> YourApp
... so if your .class file is stored in directory F:\ your classpath value
should be 'F:\' or '.' if your current working directory happens to be 'F:\'
Now do this:
F:
cd \
java -classpath . jav
can you run you class now?
kind regards,
Jos
I now get F:\>java -classpath . javException in thread "main" java.lang.NoSuchMethodError: main...Does this mean a error in my code?
indeed. It means you do not have a method whose signature is:public static void main (String xxx[])
I have deleted jav.class however attempting to run java -classpath . jav still gives me the same error....Is it to do with the code? It all looks right to me- to make sure I went and got another standard hello world thing off the internet.
Yes! Congrats, we're (mostly) on to code now :-)
If you want you can post your source code now but one thing's for sure:
The error
java.lang.NoSuchMethodError: main
means that the class you are running does not have a method called main.
To be executable, your Java class must have a method declared as this:
public static void main(String[] args) {
// your code here; this is where the Java application starts to execute
}
Lokoa at 2007-7-21 1:45:05 >

It works now.How very strange- doing java via bluej doesn't need all of that.Why does main need this (string[] args) in it? just () is usually acceptable.Now I can actually do my work....Sort of.
I assume your doing the HelloWorldApp from the tutorial.
Some things to know and remember always...
The file name of the java source must match the class name in the file.
You should strive to have 1 source file for 1 class. (until you prograess to GUI's)
In your case, your using the F:\ drive. I'll use A:\
The application code:
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
The code should be in a file named HelloWorldApp.java on you F:\ drive..
the drive looks like this:
A:\>dir
Volume in drive A has no label.
Volume Serial Number is 5422-7FFB
Directory of A:\
05/16/2006 03:33 PM283 HelloWorldApp.java
1 File(s)283 bytes
0 Dir(s)1,457,152 bytes free
A:\>
To compile I do this: javac HelloWorldApp.java
A:\>javac HelloWorldApp.java
A:\>dir
Volume in drive A has no label.
Volume Serial Number is 5422-7FFB
Directory of A:\
05/16/2006 03:33 PM283 HelloWorldApp.java
05/16/2006 03:35 PM432 HelloWorldApp.class
2 File(s)715 bytes
0 Dir(s)1,456,640 bytes free
A:\>
This gives me a class file.
To execute I do this: java -classpath . HelloWorldApp
It looks like this:
A:\>java -classpath . HelloWorldApp
Hello World!
A:\>
I hope this helps. You have had some dificulties getting things configured and running. Once you understand the relationship between classes, filenames, and how the tools work, I'm sure you'll find Java to be a great langguage.
For your help, I have a list of links to all sorts of java reference materials, free eBooks, and tutorials. Thanks to Yawmark for compiling the original. I have added to it some.
[url=http://java.sun.com/j2se/1.5.0/install-windows.html]Installation Notes - JDK 5.0 Microsoft Windows (32-bit)[/url]
[url=http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html]Your First Cup of Java[/url]
[url=http://java.sun.com/docs/books/tutorial/]The Java Tutorial - A practical guide for programmers[/url]
[url=http://java.sun.com/learning/new2java/index.html]New to Java Center[/url]
[url=http://leepoint.net/notes-java/index.html]Java Programming Notes - Fred Swartz[/url]
[url=http://www.ibiblio.org/obp/thinkCSjav/]How To Think Like A Computer Scientist[/url]
[url=http://chortle.ccsu.ctstateu.edu/CS151/cs151java.html]Introduction to Computer science using Java[/url]
[url=http://javaalmanac.com/]The Java Developers Almanac 1.4[/url]
[url=http://java.sun.com/docs/books/tutorial/java/concepts/]Object-Oriented Programming Concepts[/url]
[url=http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html]Object-oriented language basics[/url]
[url=http://sepwww.stanford.edu/sep/josman/oop/oop1.htm]Don't Fear the OOP[/url]
Free Java Books
[url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url], by Bruce Eckel (Free online)
[url=http://pdf.coreservlets.com/]Core Servlet Programming[/url], by Merty Hall (Free Online)
[url=http://pdf.moreservlets.com/]More Servlets[/url], by Marty Hall (Free Online)
[url=http://www.scism.sbu.ac.uk/jfl/jibook/]A Java GUI Programmer's Primer[/url]
[url=http://www.brpreiss.com/books/opus5/html/book.html]Data Structures and Algorithms
with Object-Oriented Design Patterns in Java[/url], by Bruno R. Preiss
[url=http://www.andamooka.org/reader.pl?section=javanotes]Introduction to Programming Using Java[/url], by David J. Eck
[url=http://java.sun.com/developer/onlineTraining/Programming/JDCBook/index.html]Advanced Programming for the Java 2 Platform[/url]
[url=http://java.sun.com/docs/books/jls/]The Java Language Specification[/url]
Books:
[url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url], by Bert Bates and Kathy Sierra
[url=http://www.horstmann.com/corejava.html]Core Java[/url], by Cay Horstmann and Gary Cornell
[url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url], by Joshua Bloch
Happy Codeing,
JJ
> It works now.
> How very strange- doing java via bluej doesn't need
> all of that.
> Why does main need this (string[] args) in it? just
> () is usually acceptable.
>
>
> Now I can actually do my work....Sort of.
Yeah, BlueJ and most all IDE's hide most of the classpath and path type stuff. I suggest you learn the nuts and bolts of how the tools work before you move to an IDE.
BlueJ is a great ide to learn the OOP concepts as it utilizes UML class diagrams when you develop. It might be overwhelming at first. So be forewarned.
Cheers!
JJ
> It works now.
Great, congrats, you're welcome.
> Why does main need this (string[] args) in it? just
> () is usually acceptable.
The String array contains any command line arguments passed when running. If you do e.g.
java -classpath . jav LookHere
Then in your main method, the first element of the array will be a String "LookHere", as in
args[0].equals("LookHere");
Lokoa at 2007-7-21 1:45:05 >
