Search code examples
javawindowsintellij-ideastdlibraries

How can I get stdlib.jar to work in IntelliJ IDEA?


I​ had used the IntelliJ IDEA installer provided in the booksite for Computer Science: An Interdisciplinary Approach and I had no problem to use the library. However, I noticed that installation had screwed my Git bash display settings, so I uninstalled everything.

Later, I installed the latest version of IntelliJ (2021.3.3), from the JetBrains website and apparently all the previous settings were overwritten, since I could no longer use the library. So, I downgraded back to the version provided in the booksite, however, I still can't use the Std library.

I've tried adding the versions of the library .jar files that I found:

Together and independently to the module and libraries, via IntelliJ IDEA, following the paths: "File > Project settings > Modules > + > JARs or directories" and "File > Project settings > Libraries > + > Java" , respectively, and choosing the corresponding directory/file, with no luck, since I've got the "cannot find symbol" error message:

Barnsley.java:16: error: cannot find symbol
        StdDraw.setScale(-0.1, 1.1);              // leave a 10% border
        ^
  symbol:   variable StdDraw
  location: class Barnsley

For literally every single mention of the Std libraries in the code. I​ also tried editing the environment system variables and created a variable named "CLASSPATH" and linked it to the Stdlib.jar file and then, to the .lift folder that came with the hello.zip file, from the first week assignment. Again, no luck, because I kept getting the same error message.

Next up, I typed:

javac -cp stdlib.jar Barnsley.java

In the command line, and although that command itself didn't lead to any error message, when I tried to run the program, typing:

java -cp stdlib.jar Barnsley 10000

This error message showed up:

Error: Could not find or load main class Barnsley Caused by: java.lang.ClassNotFoundException: Barnsley

I also vainly tried copying and pasting every .java file from Standard libraries webpage to the same directory where I keep the program files.

I know some (maybe lots of people) have asked the same or a similar question before, either here or in other forums, but I've been googling and looking for a solution for three days now and, as you can see, none of the reported solutions that I've found have been of help to me, that's why I decided to ask again, on my own.

Thank you for reading my post and/or for any help you can provide.

PS.: The Barnsley.java file in the error messages corresponds to the one provided in the 5th section of this webpage from the booksite.

[Edit - April 7th, 2022]: I tried to follow @CrazyCoder advice, so I created a folder named "src", extracted all the files from the stdlib.jar file that came with the hello.zip file into src and also moved Barnsley.java to that folder. Next, Next, I tried to follow the instructions from the webpage he mentioned, as shown in the following screenshot: New roject settings

And somehow, got the same error message when compiling: Error message screenshot

I don't know what went wrong :C Any assistance would be highly appreciated.

[Edit - Later on April 7th, 2022]:

Something weird happened. I downloaded the file provided by @CrazyCorder and there was a warning about Amazon Corretto not being installed, that I decided to ignore.

I tried to run the program from the command line, with no luck, because I got the same error message as always. So, I noticed that there was no JDK assigned to the SDK slot in the Project structure, and I chose to use JDK 18 (following the path "File > Project structure > Project"), which is the one that I had previously installed on my computer, but that didn't really change a thing.

Lastly, I tried to run it from the IDE and that's when it finally worked!!! So, thank you, @CrazyCoder :D I still don't know why it doesn't work from the command line, though...


Solution

  • I also vainly tried copying and pasting every .java file from Standard libraries webpage to the same directory where I keep the program files.

    This works just fine, you need to place all the .java files from the stdlib.jar into the sources root directory (marked in blue in the project view). Barnsley.java needs to be in the same directory.

    works fine

    The jar with .class files will not work in the dependencies/classpath as you cannot import classes from the default package in Java.

    It was an extremely poor choice to keep the classes in the default package and it's the fault of the creators of this library/course. This simple oversight has caused major headaches over the years.