You are not logged in.

#1 2009-11-24 02:13:00

mgangav
Member
Registered: 2009-04-18
Posts: 21

[Solved] CLASSPATH for javax.swing libraries?

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

#2 2009-11-24 02:22:51

urist
Member
Registered: 2009-02-22
Posts: 248

Re: [Solved] CLASSPATH for javax.swing libraries?

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

#3 2009-11-24 15:01:15

mgangav
Member
Registered: 2009-04-18
Posts: 21

Re: [Solved] CLASSPATH for javax.swing libraries?

urist wrote:

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

#4 2009-11-24 15:13:24

peart
Member
From: Kanuckistan
Registered: 2003-07-28
Posts: 510

Re: [Solved] CLASSPATH for javax.swing libraries?

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

#5 2009-11-24 15:44:03

mgangav
Member
Registered: 2009-04-18
Posts: 21

Re: [Solved] CLASSPATH for javax.swing libraries?

peart wrote:

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

#6 2009-11-24 17:44:56

urist
Member
Registered: 2009-02-22
Posts: 248

Re: [Solved] CLASSPATH for javax.swing libraries?

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

#7 2009-11-24 18:19:13

peart
Member
From: Kanuckistan
Registered: 2003-07-28
Posts: 510

Re: [Solved] CLASSPATH for javax.swing libraries?

urist wrote:

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 smile  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

#8 2009-11-24 18:59:13

urist
Member
Registered: 2009-02-22
Posts: 248

Re: [Solved] CLASSPATH for javax.swing libraries?

Thanks. Guess I should take CS II over again.

Offline

Board footer

Powered by FluxBB