Java Forum / General / January 2007
What classpath should I set so javac can find all those classes came with SDK?
Shawn - 04 Jan 2007 17:36 GMT Hi,
I have just installed Java SDK 5.0 at C:\Program Files\Java\jdk1.5.0_10.
Using Control Panel -> Systems -> Adavanced Tab -> Environment Variables, I have set PATH to C:\Program Files\Java\jdk1.5.0_10\bin;
Now I need to set CLASSPATH. I have googled and to my surprise, there is little info specific to Sun SDK. I need the exact string(s) to enter, but I cannot find it.
Right now, if I "javac MyWindow.java", there are many classes cannot be found, like Point.java, Graphics.java, etc (I have import statements at the top already).
Thank you very much.
Steve W. Jackson - 04 Jan 2007 18:22 GMT > Hi, > [quoted text clipped - 13 lines] > > Thank you very much. I hope you mean you set PATH "to include" that location, rather than "to" that location... It's also worth mentioning that any command prompt opened before you did that will not reflect the change, so you should open a new one.
Sun has long recommended against setting a CLASSPATH variable, and many here will concur (myself included). When you need to set a classpath, you should do so on the command line of any individual javac or java command -- or in the manifest file if using a jar file that's supposed to be "executable" on its own.
The command you describe -- if everything else is correct, and you don't provide enough info to determine that with certainty -- should be able to compile your MyWindow.java file provided it makes use of nothing except built-in classes and has no "package" statement, since they're all right there in various proper spots beneath the jdk1.5.0_10 directory you mention.
= Steve =
 Signature Steve W. Jackson Montgomery, Alabama
Shawn - 04 Jan 2007 18:51 GMT > I hope you mean you set PATH "to include" that location, rather than > "to" that location... It's also worth mentioning that any command [quoted text clipped - 15 lines] > > = Steve = O.K. Let me make myself clear a little bit:
I have installed Java SDK 5.0 in its default location in Windows XP. I have set PATH permanently to: C:\Program Files\Java\jdk1.5.0_10\bin
because javac.exe and java.exe are at that location.(by using Systems -> Advanced Tab->Environment Variables)
But now, if I "javac MyWindowGui.java", many classes cannot be found, like Graphics.java, Point.java, etc. I found I need to do: (1) unzip Java source file, find Graphics.java and copy to the same directory as MyWindowGui.java (2) remove the line "package ..." in the Graphics.java (3) find Point.java and do the same thing
Because MyWindowGui.java uses many other Java classes, it is almost impossible to find all the class files and copy to the same directory as MyWindowGui.java and remove the line "package ..." in the file.
I think I need to set the CLASSPATH so all of them can be found in their original location. But I don't know what the value should I set to CLASSPATH. Currently, by using System->Advanced Tab->Environment Variables, I set CLASSPATH to: .;C:\Program Files\Java\jre1.5.0_10\bin;C:\Program Files\Java\jre1.5.0_10\lib
"javac MyWindowGui.java" still cannot find all the classes they need.
Steve W. Jackson - 04 Jan 2007 19:42 GMT > > I hope you mean you set PATH "to include" that location, rather than > > "to" that location... It's also worth mentioning that any command [quoted text clipped - 44 lines] > > "javac MyWindowGui.java" still cannot find all the classes they need. As I said, if you've properly done the installation and set PATH, you do *not* need a CLASSPATH variable, nor do you need to obtain any of the source files for classes that are part of Java 5. You haven't shown any code, and that's probably the source of your error. You have an import statement for "java.awt.*", or separate ones for "java.awt.Graphics" and "java.awt.Graphics", don't you?
 Signature Steve W. Jackson Montgomery, Alabama
Shawn - 04 Jan 2007 20:58 GMT O.K. Could you look at my code to see what is wrong. It is pasted below.
<Code> import java.awt.*;
import javax.swing.JFrame; import javax.swing.JOptionPane;
/** * * This is a simple class with static methods to make it * easy to display error and message dialogs. */ public class DialogHelper {
// -----------------------------------------------------------------------------------------
public static boolean displayConfirmDialog(Window parent, String message, String dialogTitle) { int choice = 0; boolean confirm = false;
choice = JOptionPane.showConfirmDialog( parent, message, dialogTitle, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE );
if ( choice == JOptionPane.YES_OPTION ) { confirm = true; }
return confirm; }
// -----------------------------------------------------------------------------------------
public static void displayErrorDialog(Window parent, String message, String dialogTitle) {
int dialogType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(parent, message, dialogTitle, dialogType); }
// -----------------------------------------------------------------------------------------
public static void displayMessageDialog(Window parent, String message, String dialogTitle) {
int dialogType = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(parent, message, dialogTitle, dialogType); }
// -----------------------------------------------------------------------------------------
public static void displayMessageDialog(Window parent, String message) {
JOptionPane.showMessageDialog(parent, message); }
// -----------------------------------------------------------------------------------------
public static void setWaitCursor(JFrame frame) { Container c = frame.getContentPane(); // get the window's content pane setWaitCursor(c); //c.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
public static void setDefaultCursor(JFrame frame) { Container c = frame.getContentPane(); // get the window's content pane setDefaultCursor(c); // c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public static void setWaitCursor(Container container) { container.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
public static void setDefaultCursor(Container container) { container.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }
} //end class DialogHelper </Code>
Andrew Thompson - 04 Jan 2007 21:41 GMT > O.K. Could you look at my code to see what is wrong. It is pasted below. After fixing the line wraps caused by the absurdly long lines, that example compiled cleanly here.
Back to the environment, but as (I think) Steve mentioned, it will not be fixed by setting a classpath or environment variable. The symptoms you describe seem more like those of a broken installation.
Andrew T.
Steve W. Jackson - 04 Jan 2007 22:12 GMT > > O.K. Could you look at my code to see what is wrong. It is pasted below. > [quoted text clipped - 7 lines] > > Andrew T. That's what I found, too. I compiled it cleanly on my Mac, where (like the XP system I use for work) I do not have a CLASSPATH variable set.
In the original posts, there was reference to "MyWindowGui.java" rather than this code's DialogHelper.java, so it's not clear if some other code exists that isn't compiling properly. But a correctly installed JDK doesn't require anything -- even the PATH variable -- to be able to compile. You could actually type the "javac" command by providing its complete path, and the needed classes from the JDK would be correctly found.
Lotsa luck.
= Steve =
 Signature Steve W. Jackson Montgomery, Alabama
Shawn - 04 Jan 2007 23:34 GMT Thank you. Everything is working.
But I am surprised that you guys are saying that no need to set CLASSPATH and Sun doesn't recommend it either. I don't understand it.
If you don't set CLASSPATH, how do you deal with package issue. For me, if my Java class file(MyClass.java) is located at: C:\programming\project\tool\mystuff\MyClass.java
I set CLASSPATH: C:\programming\project
Then I add the line at the top of MyClass.java:
package tool.mystuff;
If I want to do it differently, say I set CLASSPATH: C:\programming Then I add the line at the top of MyClass.java: package project.tool.mystuff;
Daniel Pitts - 05 Jan 2007 00:25 GMT > Thank you. Everything is working. > [quoted text clipped - 14 lines] > Then I add the line at the top of MyClass.java: > package project.tool.mystuff; Actually, the recommendation is that you are in the correct directory to begin with.
Instead of setting CLASSPATH=....
do this: cd c:\programming\project javac tool/mystuff/MyClass.java java tool.mystuff.MyClass
Lew - 05 Jan 2007 02:22 GMT Shawn wrote:
>> But I am surprised that you guys are saying that no need to set >> CLASSPATH and Sun doesn't recommend it either. I don't understand it.
> Actually, the recommendation is that you are in the correct directory > to begin with. [quoted text clipped - 5 lines] > javac tool/mystuff/MyClass.java > java tool.mystuff.MyClass In addition to that, for more complex scenarios like adding external JARs to the classpath, use the -classpath (-cp) argument to the java and javac instead of the environment variable.
- Lew
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|