简体   繁体   中英

Running java program using packages in multiple folders

I'm trying to run a program with the following structure:

+src
    +gui
        -XL.java
        -moreFiles.java
        +menu
            -guiFiles.java
    +util
        -utilFiles.java
    +extra
         -extraFiles.java

I'm trying to compile the code by calling

javac gui/XL.java

Which succeeds. When I try running the code with

java gui.XL

I get the following error message:

Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
    at gui.XL.<init>(XL.java:25)
    at gui.XL.main(XL.java:58)
Caused by: java.lang.NoClassDefFoundError: java/lang/invoke/StringConcatFactory
    ... 2 more
Caused by: java.lang.ClassNotFoundException: java.lang.invoke.StringConcatFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

I guess I have some problems with my classpath but I have no clue about how to fix it. Does anyone have any suggestions?

Let's agree to stick to JDK 8 for now. You probably don't need cutting edge JDK 9 features yet.

You need to know how to compile all the .java files in a directory at once . I'd recommend that you learn about the command line option that tells the JDK where to write the .class files.

You absolutely have to understand how CLASSPATH works or it's not possible to write Java.

You should never set a CLASSPATH environment variable on your machine. You should learn how to set it properly every time using the -classpath option when you compile and run.

You might consider using an IDE. I hate to suggest it, because it might be overwhelming, but IntelliJ from JetBrains is the best IDE on the market. This is a simple problem if you know how to use it.

My problem had nothing to do with classpaths or that I wasn't using an IDE. My problem came from the fact that I was changing the 'java' command to run my java-openjdk-8 but my compiler still ran with java-openjdk-9. The solution was therefore

sudo update-alternatives --config javac #Change to java 8
sudo update-alternatives --config java #Change to java 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM