0

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

jfo420
  • 11
  • 2
  • 1
    How exactly are you compiling? Are you using an IDE? Have you tried writing an utterly simple minimal app? If so, post it. – Basil Bourque May 14 '22 at 17:43
  • I am compiling using command line, but anyway starting from a blank code page, only typing 'import javax.swing.*;' triggers the same kind of error in the editor I use (it's Visual Code but I think is irrelevant since even the command line compiling don't recognize any of the swing components). The program actually works great using another PC... only this time I'm having a hard time with the installation. I took care not to get the headless version of JDK. – jfo420 May 14 '22 at 18:34
  • Setting JAVA_HOME and CLASSPATH don't do much for running javac. You need to make sure you're running the correct java version and show the error you get with the import statements. Since those have to work first. – matt May 14 '22 at 19:53
  • Not to mention your trying to compile `javac name.java` but the class is "ETSFrame" – matt May 14 '22 at 19:54
  • @matt it's more to show no option was used, this didn't solve anything but it is a helluva hint. – jfo420 May 14 '22 at 20:12
  • "Visual Code but I think is irrelevant since even the command line compiling don't recognize any of the swing components" This issue comes up, https://github.com/redhat-developer/vscode-java/issues/956 which seems related. – matt May 14 '22 at 20:19
  • "this didn't solve anything but it is a helluva hint." are you talking about the answer I provided? Can you say what the exact error is when you try it? – matt May 14 '22 at 20:20

1 Answers1

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
matt
  • 10,892
  • 3
  • 22
  • 34
  • My problems is the first line, Java tells me this package doesn't exist. But I see it in src.zip, as well as into the referenced libraries of the project. By any chance do you happen to know how Java is fetching into this zip file ? Actually clueless, and thanks for the note. (issue remains) – jfo420 May 14 '22 at 20:22
  • @jfo420 it doesn't do anything with src.zip. That is the source that was used to build the java you are using. Are you really using the `/usr/lib/jvm/java-17-oracle/bin/java`? Quite honestly, I would just go and download a new one and install it locally. – matt May 14 '22 at 20:23
  • @jfo420 I don't know if oracle's JDK uses the CLASSPATH environment variable. I'm pretty sure it doesn't, but if it does I think you have it wrong. `/usr/lib/jvm/java-17-oracle/*` that points to the root of the JDK, not where any of the necessary classpath items exist. – matt May 14 '22 at 20:32
  • I have no idea, I removed all the previous builds (been trying for one week), then I let Synaptic (provided package manager) do the installation. The path it installed to seems pretty legit. But then again, `/bin` holds links to `/etc/alternatives` ... I was thinking everything would hold into one place, am I wrong ? – jfo420 May 14 '22 at 20:42
  • Yes. You need to use "update alternatives" and it will let you select the correct java. I would ditch the java_home and classpath from your .bashrc for now since at best it is doing nothing. Ill take a look for the update alternatives and link it. (I'm using a dnf based distro) – matt May 14 '22 at 20:44
  • @jfo420 https://stackoverflow.com/questions/12787757/how-to-use-the-command-update-alternatives-config-java that is pretty old, but it seems if you run that command it will tell you which java's are available and you can select the one you want. – matt May 14 '22 at 20:49
  • thanks for the link. For now I installed a new build, openjdk18, I will dig into it tomorrow with fresh head. There is a lockup somewhere, every build gives me similar result (not recognizing javax.swing). – jfo420 May 15 '22 at 00:22