This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JAVA kicking and screaming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JAVA kicking and screaming" Watch "JAVA kicking and screaming" New topic
Author

JAVA kicking and screaming

Robert Crouch
Greenhorn

Joined: Jun 06, 2004
Posts: 5
I did a little programming in PASCAL and learned C++ enough to do loops and simple arrays - now trying JAVA but I need to be a DOS expert just to get the language to run. It seems some expertise in setting CLASSPATH and such is needed just to get Hello World to compile and run. My level of frustration is at the max! I downloaded latest JAVA from SUN --- bought a sample programs CD - and can't run a dern thing! I'm using TEXTPAD as my text editor = nothing compiles - even the professional examples--- so it must be my installation on a Win XP machine...... I'm ready to abandon all hope and return to C++ unless I can get 1 program to compile and run. Tmust be a better way to get started in this language. I can't image anyone standing for this in programs they buy -- what ever happened to the plug and play theory! Guess it doesn't apply to Java language...Hanging on by a thread......
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The classpath for the current versions of j2se is the dot (".") directory, ie, whatever directory you are in. You do need to deal with the path which tells the computer where to find your sdk bin so you can compile (with javac) and run (with java) your programs.

So I would start with this. What is the address of your sdk on your computer and what directory do you want to keep your source code in? On my xp the sdk is here: c:\sdk1.4.2 and the path I use is [c:\sdk1.4.2\bin] just what's inside the square brackets.

I do my code in [c:\jexp], a folder on the hard drive. I keep my source code and the compiled class files here.

To compile your code, at the command prompt [>] navigate to your java folder with [>cd c:\jexp] type enter and you should see [c:\jexp>]. We're in the folder. Put your HelloWorld source code file in here saved with a .java extension.

Now type in [c:\jexp>c:\sdk1.4.2\bin\javac helloworld.java] and enter. Your compiled .class file should appear in the same folder or errors explaining why not in the console.

Then type in [c:\jexp>c:\sdk1.4.2\bin\java HelloWorld] enter and you should be there - success.

After this you can set your path environment variable to save typing. And later you can set your classpath variable when you figure out what you want.
Jos� Alberto Mu�iz Navarro
Ranch Hand

Joined: Oct 27, 2003
Posts: 33
It's not really about it being complicate. It's just a matter of getting to execute the javac binary. javac is the responsible for compiling your program and transforming it into a bytecode.

You have to check where that binary is and after you do it once, you'll be able to run it more easily. Now, consider downloading Eclipse or NetBeans or some other IDE will which (I think) automatically detect those binaries without the need for you to set the CLASSPATH and PATH to the binaries.

Now, supposing your bin directory is in C:\j2sdk then to compile osmething you'd only need something like

Robert Crouch
Greenhorn

Joined: Jun 06, 2004
Posts: 5
Thanks for the advice - but I was able to edit my autoexic.bat file in WIN98 - but now that I'm using WIN XP- no dice. I can't even find the thing!
Textpad seems great - but each to their own. I can't begin to tell you how frustrating it is to read manuals - write programs - and learn that I can't even install the language right for the stuff to compile in the first place.
I have everything in C:\java
and want to store my original code programs in C:\java\myStuff
but I still can't get anything to comile - since I can't figure out how to let the computer find the java\bin. I may be hopeless ... if just getting the language to actually run is this hard - why bother with it?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Robert Crouch:
Thanks for the advice - but I was able to edit my autoexic.bat file in WIN98 - but now that I'm using WIN XP- no dice. I can't even find the thing!
Textpad seems great - but each to their own. I can't begin to tell you how frustrating it is to read manuals - write programs - and learn that I can't even install the language right for the stuff to compile in the first place.
I have everything in C:\java
and want to store my original code programs in C:\java\myStuff
but I still can't get anything to comile - since I can't figure out how to let the computer find the java\bin. I may be hopeless ... if just getting the language to actually run is this hard - why bother with it?


Giving up is your choice. But on Windows XP you need to adjust your path so that you can run javac.exe and java.exe from anywhere you want. Otherwise you can just specify the absolute path. So let's begin with that, using the absolute path:

All you need to do at the command prompt is type in the entire path to your javac.exe and java.exe executables. So lets say you have java installed at c:\java and you are running programs in c:\programs. Here is the following command to compile a HelloWorld.java program. I will assume you are already in the c:\programs directory.

c:\java\bin\javac HelloWorld.java

Now let's run it.

c:\java\bin\java HelloWorld

Unless you are using specific 3rd party libraries that are not part of the standard JDK, there is no need to specify or adjust the CLASSPATH so for now, forget about the CLASSPATH. Pretend it doesn't exist.

For the other way we want to adjust our PATH environment variable so that we don't have to specify the absolute path. In Windows XP there are a few steps.

  • Right Click on the My Computer Icon and choose Properties
  • Click on the Advanced Tab
  • Click on the Environment Variables Button


  • At the top you will see user variables and the bottom are global variables. The easiest thing to do is find the PATH variable in the list of global variables and edit it. At the end put a ; and then assuming the same directory structure as above type c:\java\bin. Click OK, and the close all the dialogs that are open. If you had a DOS prompt up you may need to close it and re-open it for the changes to take affect.

    Now, all you have to type is:

    javac HelloWorld.java
    java HelloWorld

    If you do either of the ways I suggested and are still having problems you might want to tell us what errors you are getting. Ranting and raving about how mad and frustrated you are isn't going to help anyone. Take a deep breath and, well, breathe.

    Remember, the CLASSPATH isn't important right now.


    GenRocket - Experts at Building Test Data
    Robert Crouch
    Greenhorn

    Joined: Jun 06, 2004
    Posts: 5
    Eureka--- I wiped everything out - downloaded fresh - right clicked on MyComuter- checked the Advanced tab - and the environment varibles- saw the class path as only being C:\java so I hade to edit it to be C"\java\jdk\bin and closed everything
    I ran textpad -- loaded a program - and it compiled and then ran ! I feel relieved - thanks for all the help -- and I have a feeling that I'm going to be a frequent visitor to the saloon--- what great friends I've found already. My goal is to become knowledgeable enough to one day help someone else! Thanks guys!
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15299
        
        6

    Originally posted by Robert Crouch:
    saw the class path as only being C:\java so I hade to edit it to be C"\java\jdk\bin...


    FYI - This is not the CLASSPATH. It's just the PATH to the Java bin directory. The CLASSPATH is used to tell the VM where to find libraries that are not part of the core set of libraries. You won't need to worry about this for a bit while you are picking up the basics. I just don't want you to become confused early on with termonolgy.

    CLASSPATH != PATH.
    Robert Crouch
    Greenhorn

    Joined: Jun 06, 2004
    Posts: 5
    Thanks once again -- and you're right--- although I can now compile and run elementary programs ... whenever I need to import -- no go! So now I have to set the classpath? Same place in WinXP - new line or add on to path line? Whatever details will be appreciated ( thankX ahed of time )
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15299
        
        6

    Originally posted by Robert Crouch:
    Thanks once again -- and you're right--- although I can now compile and run elementary programs ... whenever I need to import -- no go! So now I have to set the classpath? Same place in WinXP - new line or add on to path line? Whatever details will be appreciated ( thankX ahed of time )


    What do you mean "no go"? Are you getting compilation issues? Runtime issues? What classes are you importing?

    The best way to specify the classpath is at compile and runtime. Don't worry about setting environment variables for this. just use the -classpath switch on the executable. Example:

    javac -classpath extralib.jar HelloWorld.java
    java -classpath extralib.jar HelloWorld

    Where extralib.jar is the other libraries not included with the JDK. I find it hard to believe you are using extra libraries yet. You might also post your code.
    Sal DiStefano
    Ranch Hand

    Joined: Aug 28, 2002
    Posts: 90
    Just FYI

    If your not a command line person, under Windows XP the environment variables can be viewed from the System Properties window which can be found from My Computers -> Properties -> Advanced Tab. On the bottom select Environment Variables and you will be able to set and view User and machine defined information.

    Sal
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JAVA kicking and screaming