You are not logged in.
Hello,
I wanted to use the swing packages provided by java for developing cross platform GUIs. But, when I tried compiling it, I ended up with the following error:
HelloWorldSwing.java:1: package javax does not exist
import javax.swing;
After searching a bit online, I found out that the CLASSPATH variable needed to be set to use external java libraries. (I installed the jdk and jre packages). I tried setting CLASSPATH to /opt/java/jre/lib in my ~/.bashrc but that didn't help. What should I do to remedy this situation?
(This is the first time that I am trying to use external libraries like swing on Arch. So I am a Noob at this)
Thanks for your time!
Last edited by mgangav (2009-11-24 15:45:59)
Offline
All I did was add /opt/java/bin and /opt/java/jre/bin (not sure if it's required) to my PATH and set JAVA_HOME to /opt/java/jre and swing works just fine for me.
I'm unsure if any of this helps, though.
Last edited by urist (2009-11-24 02:24:50)
Offline
All I did was add /opt/java/bin and /opt/java/jre/bin (not sure if it's required) to my PATH and set JAVA_HOME to /opt/java/jre and swing works just fine for me.
I'm unsure if any of this helps, though.
Thanks for your reply! But, for some reason, this hasn't helped me. Do you think that there are some special package that I have to install. Do you have a CLASS_PATH variable?
Offline
mgangav,
If your line of code really reads "import javax.swing", then that is the problem, not the classpath. After installing the JDK, all you have to do is log out and back in to get a proper classpath for Swing applications.
What you should do in your code is import either all of javax.swing (with "import javax.swing.*"), or just the classes you need (for example, "import javax.swing.JTextArea"). Importing does not work with just a package name. You must supply either the wildcard or a specific class name.
Offline
mgangav,
If your line of code really reads "import javax.swing", then that is the problem, not the classpath. After installing the JDK, all you have to do is log out and back in to get a proper classpath for Swing applications.
What you should do in your code is import either all of javax.swing (with "import javax.swing.*"), or just the classes you need (for example, "import javax.swing.JTextArea"). Importing does not work with just a package name. You must supply either the wildcard or a specific class name.
*Slaps forehead* Darn it. You were exactly right! Once I changed my lines of code to read as below, I was golden.
import javax.swing.*;
import javax.swing.event.*;
Thanks for your help. It's the helpful people in a forum that makes any particular Linux distro as user friendly as it is!
Offline
The
import javax.swing.event.*;
is unnecessary as it is covered by the previous import.
That is a forehead slapper. I'm surprised I missed the original problem.
Last edited by urist (2009-11-24 17:45:19)
Offline
The
import javax.swing.event.*;
is unnecessary as it is covered by the previous import.
No, each package must be imported separately, even if one is "inside" the other. I wrote a little test program to make sure Try compiling it with line 4 commented/uncommented.
package foo;
import javax.swing.*;
//import javax.swing.event.*;
public class Thingy
{
public Thingy() {
JMenu menu = new JMenu("File");
menu.addMenuListener(new MenuListener() {
public void menuCanceled(MenuEvent e) {
System.out.println("canceled");
}
public void menuSelected(MenuEvent e) {
System.out.println("selected");
}
public void menuDeselected(MenuEvent e) {
System.out.println("deselected");
}
});
}
}
Offline
Thanks. Guess I should take CS II over again.
Offline