'Swing can't be found
I have this version of Java installed on a Linux Mint 20.3 Una :
java 17.0.1 2021-10-19 LTS Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39) Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
Everything works fine except when I tried to import javax.swing.*, JAVA_HOME and CLASSPATH are well set in my .bashrc configuration file.
The installation resides on /usr/lib/jvm/java-17-oracle if it can be of any helpful information. I am out of clues, would there be any other file that is supposed to carry important data ? How to get around this ?
My error is that the package is physically on disk but can't be found by the compiler. (I have veriified inside the src.zip file)
Here is my piece of .bashrc
JAVA_HOME="/usr/lib/jvm/java-17-oracle/bin"
export JAVA_HOME
CLASSPATH="/usr/lib/jvm/java-17-oracle/*:~/java/saxon/saxon-he-11.3.jar::~/java/saxon/saxon-he-test-11.3.jar:~/java/saxon/saxon-he-xqj-11.3.jar"
export CLASSPATH
When compiling with command line javac name.java I get error on every call of every swing component looking like this :
symbol: variable BorderLayout location: class ETSFrame ETSFrame.java:103: error: cannot find symbol c.add(pan2, BorderLayout.LINE_START);
And the first error I get is :
package javax.swing does not exist (compiler.err.doesnt.exist)
After that point, Java can't resolve anything relative to swing package. The project includes all the base Java packages, like java.desktop ...
Edit 1 :
I made a new installation with OpenJDK 18.0.1, also there was a file that wasn't updated to this new JDK, /etc/profile.d/jdk.sh but even with that updated my problem remains.
Files edited:
/etc/profile.d/jdk.sh/etc/profile.d/jdk.csh/etc/profile
Solution 1:[1]
Since you're using java 17 you don't even need to run the compiler directly. Running java on a simple class file will invoke the compiler implicitly and then execute the main method.
import javax.swing.*;
public class HW{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.add(new JLabel("hello world"));
frame.pack();
frame.setVisible(true);
}
}
Save that to a file HW.java and run /usr/lib/jvm/java-17-oracle/bin/java HW.java
On a separate note, the JAVA_HOME should point to the directory above bin.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Basil Bourque |
