Java and the Windows Command Prompt
This page is obsolete.
This document instructs you on how to use the Windows Command Prompt with Java. These instructions are specialized to Windows 7, but are similar for Windows XP and Windows Vista.
Java |
- Download and install the latest version of the Java Platform, Standard Edition Development Kit (Java SE 6 Update 27). Note the installation directory for later—probably something like C:\Program Files\Java\jdk1.6.0_27\bin.
-
To make sure that Windows can find the Java compiler and interpreter:
-
Select Start -> Computer -> System Properties -> Advanced
system settings -> Environment Variables -> System variables ->
PATH.
[ In Vista, select Start -> My Computer -> Properties -> Advanced -> Environment Variables -> System variables -> PATH. ]
[ In Windows XP, Select Start -> Control Panel -> System -> Advanced -> Environment Variables -> System variables -> PATH. ]
- Prepend C:\Program Files\Java\jdk1.6.0_27\bin; to the beginning of the PATH variable.
- Click OK three times.
-
Select Start -> Computer -> System Properties -> Advanced
system settings -> Environment Variables -> System variables ->
PATH.
Command-line interface |
-
Launch the command prompt via
All Programs -> Accessories -> Command Prompt.
(If you already had a command prompt window open, close it
and launch a new one.)
You should see the command prompt; it will look something like:
Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved.
-
To check that you have the right version of Java installed,
type the text in boldface below. You should see something similar to the
information printed below.
(It's important that you see the number 1.6 or 1.5
for the Java version number, but the rest is not critical.)
C:\Users\username>java -version java version "1.6.0_27" Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0_27-b07) Java HotSpot(TM) Client VM (build 1.6.0_27-b13, mixed mode, sharing)
C:\Users\username>javac -version javac 1.6.0_27
-
Since you will be using the Command Prompt frequently,
we recommend customizing the default settings.
Right-click the title bar of an open Command Prompt window,
select Properties and then:
- Set Layout -> Screen Buffer Size to 80 x 500.
- Select Options -> Edit Options -> QuickEdit Mode.
- Select Options -> Edit Options -> Insert Mode.
Compile the Program |
-
From the Command Prompt, navigate to the directory containing
your .java files, say C:\introcs\hello, by typing the cd
command below.
C:\Users\username>cd c:\introcs\hello C:\introcs\hello\>
-
Assuming the file, say HelloWorld.java,
is in the current working directory, type the
javac command in boldface below to compile it.
C:\introcs\hello\>javac HelloWorld.java C:\introcs\hello\>
Execute the Program |
-
From the Command Prompt, type the java command below.
C:\introcs\hello\>java HelloWorld Hello, World
Input and Output |
If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-z for EOF (end of file). On some DOS systems the first line of output sent to the screen after you enter EOF will be rendered invisible by DOS. This is not a problem with your code, but rather a problem with DOS. To help you debug your program, we recommend including an extra System.out.println(); statement before what you really want to print out. If anyone knows of a better fix, please let us know!
Troubleshooting |
When I type, "java -version" I get an error. Check that you edited your PATH environment variable as indicated. A missing ; or an added % is enough to screw things up. Close and re-open a command prompt. Type path at the command prompt and look for an entry that includes C:\Program Files\Java\jdk1.6.0_27\bin;. Check that the version number 1.6.0_27 matches the one you installed—Oracle updates Java periodically and you might have a more recent version. If this doesn't fix the problem, check if you have any old versions of Java on your system. If so, un-install them and re-install Java.
The command "java -version" works, but not "javac -version". Any thoughts? It's likely a path issue. Try the suggestions from the previous question. Also check that you installed the JDK properly by checking that the folder C:\Program Files\Java\jdk1.6.0_27\bin exists.
How can I check the values of my PATH variable? Type the following at the command prompt.
The PATH variable should begin with C:\Program Files\Java\jdk1.6.0_27\bin; Be sure to open the command prompt after you have edited the PATH environment variable. You may also need to reboot for the environment variable change to take effect.C:\introcs\hello\> echo %PATH%
I can compile with javac, but I get the error message "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld" when I try to execute it with java. First, be sure that HelloWorld.class is now in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. Check that the command "java -version" works. Now try to execute with "java -cp . HelloWorld". If this works, you need to edit your classpath. (iTunes has a proclivity for changing the classpath, so if you recently upgraded iTunes, this is likely the source of the problem.)
Where can I learn more about the Windows command line? Here is a short tutorial on the Windows command prompt. Microsoft maintains a complete command line reference.
How do I change my directory to the H: drive from the Windows Command Prompt? Type H: at the command prompt. Then cd to the appropriate directory.
SUMBER : http://introcs.cs.princeton.edu/java/15inout/windows-cmd.html
0 komentar:
Posting Komentar