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:
On the Standard Libraries Princeton's webpage
On the Jar Download website
On this post from Stack Overflow
In the .jar file from the hello.zip file I found on this other Princeton webpage
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:
And somehow, got the same error message when compiling:
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...
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.
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.
Related
So I've made up a little programm for my mom to use. I exported it into a runnable .jar, which is a first for me, so I obviously screwed it up.
Actually the file works just fine on my computer. But when I try it on hers, double clicking on the file just gives me an error : "could not find Main class : controller.Main". And the programm won't start
I used eclipse GUI to export it (right click on projet -> export -> runnable JAR), and it gave out a file working well on my computer.
If it is of any help, my programm uses a bunch of resources files, but I included them in the right place in the folder that I gave to my mother so it should not be the problem (besides, it would glitch out when trying to use the images, but not stop it from launching, according to some personnal tests)
What can be the Reason for it to work on my device, but not another one (both Windows) ? And how to fix this ?
N.B: I have included a MANIFEST.MS file in my project before exporting, but Eclipse didn't make any explicit mention to it when exporting, so I think it didn't take it into account. Besides, I reffered to my main class as Main (and not controller.Main as in the error message I got on my mother's computer). This was probably wrong of me, but I think it shows that Eclipse didn't care much for my manifest and kinda made his own.
Thanks in advance !
If you want a link to download the folder, to check it out yourself, let me know ! (it's a bit heavy though, there's quite a lot of images)
EDIT : I get different error messages with different computer. One computer would give me this first message :
first computer error
Another computer gives me those 2 error messages (one after another)
2nd computer error (1/2)
2nd computer error 2/2
And of course, it still runs fine on mine, just with a double click !
My .jar file (opened as an archive) contains :
3 folders corresponding to my packages : controller, model, view
1 folder META-INF containing the MANIFEST.MF generated by eclipse, looking like so :
Manifest-Version: 1.0
Main-Class: controller.Main
Class-Path: .
Ok ! I could figure it out. So there were actually 2 problems on top of each other :
first, I think Martin's suggestion was correct. After researches on the new error message, it became clear that native calls had probably had Something to do with the problem, although I have no idea what native Library I was using. I could solve this first part of the problem by selecting another option to handle libraries in the eclipse export feature : I chose the option "package required libraries into genereated JAR" (instead of "extract required libraries into generated JAR" before), and it make the error message go away !
Even though the message disappeared, the app would still not launch. But the problem was more obvious this time; I Simply compiled and exported the file with a java 9 JDK for eclipse, while trying to run it on a java 8 software (I think java 9 is only available in its developper version to this day). So I just downloaded java 8 JDK, configured the build path to use the latter, and it finally worked on the other computer.
Thanks to Martin who helped me get on track with the right questions !
I creating a program to work with databases and I am getting the following error when compiling in IntelliJ IDEA. Does anyone why this is happening and how I could solve it?
The error that you get occurs not on complilation, but when you try to run your application. It happens because Java was not able to find Table.class file inside db subdirectory of the project output directory (classpath).
It can happen for multiple reasons:
wrong main class selected in the run/debug configuration
Table.java is excluded from compilation (by accident or intentionally because it contained errors and you wanted to skip it while working on other code)
class not compiled because Build step is excluded from from Before launch steps in the Run/Debug configuration
project is misconfigured and there is no Source root defined for the directory containing db subdirectory
Table.java has incorrect package statement or is located/moved to a different package
project path contains a colon : on Mac/Linux or semicolon ; on Windows, it's used to separate the classpath and will render the classpath invalid. See this thread for details. Note that Finder on Mac may display colons in the path as slashes.
the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details.
In project structure make sure you have the right Java version for compile.
there is a known bug that sometimes a Java project created from the Command Line template doesn't work because .idea/modules.xml file references invalid module file named untitled104.iml. Fix the module name manually or create a project from scratch and don't use a template.
on Windows "Beta: Use Unicode UTF-8 for worldwide language support" Region Setting is enabled. See IDEA-247837 for more details and workarounds.
When IntelliJ IDEA is configured to store module dependencies in Eclipse format source root configuration is lost due to a known bug. Configure the module to use IntelliJ IDEA format dependencies as a workaround.
In a properly configured project and with the correct run/debug configuration everything works just fine:
the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details.
I must again emphasis the point CrazyCoder has here.
The (Oracle) JVM used to throw a SecurityException when you tried to run a Jar-File containing broken signatures. This made sense from a "What's wrong"-Point of view.
That is no longer the case. They are indeed throwing ClassNotFoundExceptions now - even if the class is right there in the file (no matter if it is in the default package/toplevel or way down in a nested package structure).
Here's what worked for me:
I deleted .ide folder, .iml file. And all other auto generated files by intelliJ then restarted my ide and I was asked if I want to make my project run with maven that's it.
Obviously I said yes :)
This is a known bug in the IntelliJ idea.
To fix this I just deleted the .iml and the .idea and restart the IDE.
It works for most of the cases
Edit: The files will be in the project directories.
In my case the default console app template works only if the project folder path does not contain underscore (_) in it. Underscore brings the error
Error: Could not find or load main class com.company.Main
Caused by: java.lang.ClassNotFoundException: com.company.Main
IntelliJ IDEA 2021.3.1 (Ultimate Edition)
Build #IU-213.6461.79, built on December 28, 2021
If you've tried everything else that others have suggested (deleting .idea folder, rebuild, etc) there's another place to check, especially if you've built an artifact jar. When you first build an artifact jar, IntelliJ adds a folder: META-INF to src directory. in it is a single file: MANIFEST.MF which has info pointing to the Main-Class for Java to find. If you've refactored your project package, unfortunately IntelliJ does not update this file with the new changes. My MANIFEST.MF has the following correct content:
Manifest-Version: 1.0
Main-Class: org.umoja4life.fatashibackend.MainKt
Where "org.umoja4life.fatashibackend" is the package name, and "MainKt" is IntelliJ's constructed name for a (pseudo) "Main Class" because fun main() has been defined in file "main.kt" in the package directory.
Newbies: btw, This will be confusing for you because there should be no actual "class Main {}" definition despite the error message stating there should be.
Before I discovered this file and after trying everyone else's suggestions, I found it quickest to just have IntelliJ start a project (with correct package name!), initialize it with a trivial main.kt having:
fun main() { println("hello world!") }
run and test that; then, I added back in all my other files, rebuilt, ran, and tested it. Apparently IntelliJ has some secret state information stored somewhere which doesn't get correctly updated if your refactor your package name for an already running project and jar.
I'm new at java programming so i just wanted to try something with ssh understand how it works and so i downloaded a java library which is mentioned in this Stack Overflow answer
. After a lot of failed attempts on importing this library into netbeans. I decided to simply put the files into my src folder. But Netbeans ide duplicating packages in the jar file with their 'name equivalent' empty packages and this prevents me to reach the classes in the packages because the IDE and the Code completion automatically sees the empty packs.I've search the network couldn't find anything about this problem.I 'm ready to share any log files if there are any of them .Here is my screenshot about this situation.
Screenshot Attachment
OS : Linux Mint Rosa
IDE : Netbeans 8.1 Linux version
Edit : I Already looked these topics
How to use .jar files in NetBeans? : At first i've tried this and in this solution netbeans not duplicating any empty packs but when i start to type somethin' like that import sshj.foo.*; Auto Complete can't find anything and also if i try to write the whole path to a specific class which is in an specific pack IDE gave me an error the pack that you're looking for is not exist.Secondly I've tried this
and got the same issue at first try.I can see the packs,classes and other stuff but still can't reach them in a random class which is imported project.
How can I include external jar on my Netbeans project : I've tried this and also this ran perfectly without errors as they said but when i try to reach classes still got the same problem so nothing changed.
How to add a JAR in NetBeans :
In this topic all of the replies explained types of libraries.That's not even related to my problem
How to use classes from .jar files? : I've tried to do explained in this reply also and I've transfered my jar file into a compiled .class extension file and this didn't solve my issue too.
I've just solved that problem with downloading part of the libraries from here.
All libraries need to contain three kind of jars ;
1 . Compiled files containing jar (with extension .class)
2 . Source files containing jar (with extension .java)
3 . Javadoc files containing jar (optional)
Open a project find libraries folder on ide right click and choose Add JAR/Folder specify the directory that contains the compiled files.After importing library hit right click over it and choose edit then specify the source files and javadoc files directories you are ready to roll.
I exported a Java project of mine from Eclipse to a JAR File, and after searching the internet for an hour, have still not resolved my problem. The jar file works on the computer that I compile, and exported it on, yet when I transfer it to my laptop it states that "A Java Exception has occurred" when I try to double click it, and when I run the code below, it retorts "Error: Could not find or load main class jar"
java -jar C:\Users\Me\Desktop\test.jar
I exported it once more after specifying to the MANIFEST.MF the location of my main class, yet the same error is being reported. I have seen the MANY other threads on this on this forum, yet the advice has either been too vague or just plain didn't work.
Thank you in advance.
Check the version of java you are running in your laptop, the error could present itself if you compiled your program with a different version that the one you are using to run it.
I am new to Java and I am not sure how apt is my question title. Please suggest any better title.
I have got a Java API which has many executable modules which do various things related to parsing MS Outlook's .pst files. And my problem is that I am getting this exception when I run the execute command to execute a .class file of one module of the API(java -classpath /opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example)-
Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example
Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example. Program will exit.
So far ...
You can check my previous question where I concluded that I was correctly running the compile and execute commands. But I am still getting the above mentioned `exception.
I asked the API's developer who says that I should get Eclipse or Netbeans IDE. He said that the
examples folder inside the API's
folder structure is entire Eclipse
workspace.
and also said that
It is easier to compile and run all
examples.
And now ...
So, I have setup Eclipse Helios for Java. Can anyone tell me how is it easy to compile and run all the examples (I think the modules inside the examples folder) using the IDE. I have earlier worked with Eclipse for PHP but never for Java.
Folder structure of the API
I need to execute a .class file of one of those modules (many module folders are present inside examples directory). There are many such modules present inside the examples folder of the API. Each of these modules folders contain a bin folder (containing .class file) and src folder (containing .java file) . I need to execute one such .class file.
There is a lib directory containing a .jar file (which is needed to be referenced by my target .class file). The lib directory is at the same level as the examples directory.
Solution
I have verified that the solution given by Jonathan works (verified only without-using-elipse case). To make it more clear the classpath needs to include all the class locations in both compile and execute commands. So, if you need to compile and execute an Example.java file with a dependency Parent.jar do this:-
Compile
javac -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.java/file/ Example.java
Execute
java -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.class/file Example
And note the separator - should be : for linux and ; for windows.
Thanks,
Sandeepan
When Eclipse starts it should ask you which workspace to use. Select the examples folder and, if it really is an eclipse workspace, you should see all the projects already set up (it may take some time to build everything depending on the size of the projects and the speed of your machine). Then simply find the class you want and run it. If everything is set up correctly it should just work. If not you may have to go back to the developer for more details.
But I don't think you need Eclipse to run that one class. Looking at the stacktrace you provided at the beginning of your question it looks like it failed to run because you haven't set your classpath properly. Try:
java -classpath /opt/Java/libs/JPST/lib/jpst.jar:/opt/Java/libs/JPST/examples/GetInboxMessages/bin Example
To very briefly explain: The classpath lists the places where Java looks for the classes it needs. You specify the class to run relative to the classpath, not as a file path. Since the class you are trying to run in in the folder "/opt/Java/libs/JPST/examples/GetInboxMessages/bin" this folder must be on the classpath (note, I am assuming a unix based system, if you are using windows use ';' to separate entries not ':'). Then simply specify the class to run by itself relative to the classpath (which in this case is just the class name by itself).
Please be aware this is a very basic overview, things get a more complex if you start using packages (folders that would appear below the bin folder). This is one advantage of using a program like Eclipse since it will handle a lot of this for you.
Sounds like a classpath issue.
Right click on your project in eclipse.
Go to buildPath > configure build path
Make sure the jar file is referenced in the libraries tab and if it isn't add it.