Helpful Information
 
 
Category: Computer Programming
SDK Question

Hello

I recently downloaded SDK 1.4.1 from java.sun.com. I've read a couple tutorials on how to use it.

Write your code into (in my case) notepad. Save it as HelloWorld.java to the bin directory of j2sdk1.4.1. Open dos, navigate to the j2sdk1.4.1 bin folder, and type in javac HelloWorld.java.

I've found a problem with this, if it is correct. (Or a problem on my computer anyway)

When I'm in the C:\ I see j2sdk1.4.1, yes. I then type cd j2sdk1.4.1 and hit enter. It gives me an invalid directory error. I have tried to move HelloWorld.java to a different directory, but javac is not a 'recognizable' command. I've also tried to rename j2sdk1.4.1, and it gives me an error, that some programs may not funtion properly if the directory is renamed.

My question is how do I compile and run a .java file?

Aerzan

Ahh can see I'm not the only one learning java :) We could learn from each others threads here :D

The SDK never worked for me either, so I got the jdk1.1.8 This works perfectly for me. You put your .java file into the bin directory, didn't seem to work if I put it in a directory in the bin directory (ie jdk1.1.8\bin\test)

I also found a better site. Teaches you more of what you're coding than assuming you know everything

http://www.javacoffeebreak.com/

I used the HelloWorld tutorial from there and it worked :) The java.sun only worked after I took public off the first line

I downloaded JDK1.1.8, and read the instructions on javacoffeebreak.com.

I edited my autoexec.bat to include the path of jdk1.1.8 bin and restarted my computer.

I'm afraid to say that it did not help. I created a HelloWorld.java file, saved it into the bin, and tried to access it with cd jdk1.1.8, but it gives me an invalid directory error for some reason.

I don't know what I'm doing wrong.

Aerzan

are you going to C:\WINDOWS\jdk1.1.8 or C:\jdk1.1.8? The last one is correct.

maybe a problem with long file names?

For what it's worth:

Here's what I do and it works for me:

* Write my .java file and save it to a folder located at c:\javafiles
* Open a dos window and at the prompt I type: PATH = c:\jdk1.3.1\bin
* Navigate to the directory of the .java files (c:\javafiles)
* At the prompt type: javac whatevername.java

Everything works for me with this method.

Smeagol
http://www.javascriptsolutions.com

Thanks for all your replies.

@ NightFire

Yes, I am using the C:\ and NOT C:\WINDOWS\ jdk1.1.8 is located in C:\ not C:\WINDOWS (or not for me anyways)

@ Whackaxe

I think you might be right, and that I cannot use jdk1.1.8 on my Windows XP (Home Edition :( ) If this is true, it is quite sad that Windows would come out with a computer like this. All I can do is wait until I earn up enough money and get a custom windows 98 and Linux Mandrake.

@Smeagol

I tried your method, and sorry to say, but it does not work. What it does (after I type path = C:\jdk1.1.8) is nothing. It stays in the same directory, and gives me no message. No error message, or anything...nothing.

If anyone else is running on windows XP (Home Edition) and has somehow gotten this to work (If you have, congratulations! :thumbsup: ), please help me.

Thanks.

My guess is XP doesn't like the .1.2.43..5.6.4.23 in the directory name. Try this. Navigate to the directory and rename it to javaDev.
This should allow you to run the javac command from the bin directory. Just cd to c:\javaDev\bin

If that doesn't work try modifying the PATH variable like this.
From the command prompt type:
set PATH = %PATH%;c:\javaDev\bin

Then javac should work from anywhere on your machine. If, again, that doesn't work. Go to the top of a really tall building, with your computer. Open a window (not windows, just a window) and throw your computer (with windows) out the window. While your computer is falling, make sure to scream "Die Bill Gates Die!". It may not fix your java problem, but I think you'll feel better. :D

@bcarl314

thanks! It worked! I went into ms dos, navigated to C:\javadev\bin, and typed javac helloworld.java. It created a HelloWorld.class file in my bin directory.

Now I have 2 more easy questions if you don't mind.

1.) Is it possible to put my java files in a different directory and compile them there? If so, how?

2.) How do I view/run my compiled program? (In this case, helloworld.java - I want to see it say 'Hello, World!' just to have the satisfaction of knowing I'm a java programmer (Not a very good one, yet))

Thanks everyone for all your help!

To see the compiled file, go to the bin directory, then type

java HelloWorldApp.class

I went into dos, and typed: cd C:\javadev\bin. This took me to my java dev bin. I maded sure that HelloWorld.java and HelloWorld.class were in my bin. I typed java HelloWorld.class, and it gave me the error: Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class.

Then I tried java HelloWorldApp (because that's what you typed).class, and it gave me the same error.
I checked to make sure that the code was right, and it was. I looked up the same tutorial on time, copied and pasted it, and it still gave me the same error.

btw, sorry for being such of a "Newbie", but I really REALLY want to learn Java.

Don't worry about it I've not got past the hello world script myself as I've not had time. I only did the hello world script last week. I have no idea why it won't work on your pc either. I'm just as confused as you

lol!

I'll probably just continue in the tutorial then...
I'm learning C++ at school, and I just downloaded the compiler, so I can learn Java and C++ at the same time! Hey, School is actually useful! lol! Well, best of luck to you. (and me for that matter)

Aerzan

Just a note, the java command to run a java class file does not need the .class extension. In fact it won't work with it.

To run a java class file simply type
java myClassFile
and not
java myClassFile.class

I beleive if you type java myClassFile.class it looks for a file in the package (ie directory) class.

Also, to run the commands from anywher (in Windows) simply got to your dos a type the following
set PATH=%PATH%;c:\pathToJavaCommands

(ie
set PATH=%PATH%;c:\javadev\bin
)

good luck

So after you set the path, you don't have to go to the bin directory all the time in dos? you can just type java HelloWorldApp at C:\WINDOWS?

Yes, once your environmental variable PATH includes a link to the java folder, you can access all the applications (javac, java, javadoc, etc) from anywhere on the system. It's like creating a file association in Windows where all .htm files will lauch IE.

It's a neat little trick in Wondows.










privacy (GDPR)