IntelliJ can't resolve symbol despite adding library - java

I am creating an external library to share code between my client/server programs. IntelliJ can't seem to import this jar file and I can't understand why, I followed all the advice I've found online.
I added the jar to my lib folder and added it as a library in my main module (it's called 'common':
This is the error (repeated on each instance of me using User or Packet). On import statements it says "package common does not exist":
I've tried invalidating caches and that didn't seem to work either. I tried this same import on a new project just to see if I was doing anything wrong and it worked perfectly on that new project so I'm not sure what's going wrong here.
EDIT:
I've noticed that this error only happens in files that are in packages within the src folder. So basically, Main can find the jar because it is only in the src folder, but RegistrationController can't because it's in src/communication.

The problem was that the shared code I was making was not in a package, just a src folder. Because of that, the code that was in my default package (src) could access the classes, but not the code in packages. To fix this, I put the shared classes in a package (com), exported them as a jar, and imported that into my project. Now, my packages can do com.Packet or com.User and get the files successfully.

Related

Android Studio Subdirectories/packages suddenly cannot find R.java?

II updated AS to the latest stable build and all of my projects melted down. Only Java files in the main directory (com.company.app) can find R.java as intended. ALL of my subdirs can't find it. I did not have to import R.java before the update. I rebuilt my projects from scratch, restored backups, copy-paste to new projects from the backups, reinstalled AS, restarted PC, invalidated caches and restart, cleaned, literally every go to insta-fix-it solution that the internet always recommends and still I can't get this to work.
Can someone tell me how my subdirectories cant find R.java but other files in my main directory can? Is this a feature change in AS or something? Are we now supposed to import R.java? There are no XML errors. My project built before I moved any subdirectory files back to my main package. The package name exactly matches the one in my manifest.
Edit 1: I can rebuild my project and make R.java. The main package files still see it without the import, but the subdirs can't find it.
Edit 2: I can move a file from my main package to a subdir and it still finds R.java. This appears to be a compiler glitch.
Edit 3: Importing R.java makes the errors go away so I guess that is a solution. I am leaving the question open because this makes no logical sense.
You need to understand how App resources work.
When you build your android project, all of your resources in /res folder will be mapped automatically in generated your.package.names.R class.
Now you want to access the resources through the class. It's only make sense when you need to import the file when you use it in different package.
I had this issue, even though the error wasn't being underlined in red there was an error in my strings.xml. Go to Values->strings.xml and check if you duplicated any strings.

Java import error - org.jdom

I am attempting to edit code from an old developer.
I know it is bad practice but what she said I had to do was create a dummy project and then copy paste the packages in question modify what I need to modify then compile and copy the classes that I changed into the directory where the classes in use are stored.
So I created the dummy project and copied the packages/files into the source folder of the dummy project but I keep getting a "package org.jdom does not exist" error.
The error appears on the lines:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
When I copied the folders/files into the source folder I copied the entire "org" directory.
You can see from my screenshot that the package is in the Project Explorer
I have several things to resolve the issue but none seemed to work.
I tried adding the "org" directory into the classpath environment variables.
I tried adding the "org" directory as a library to the project.
I have tried clearing the NetBeans cache after doing both of these but none of these 3 things worked.
Below is a screenshot of the projects src folder in file explorer
Can someone explain to me what I need to do to get this issue resolved?
I was adding the wrong files to the libraries section of my project.
I was adding the source for jdom when i should have been adding the jdom .jar file.
Thanks for the help guys.
install jdom at your library folder, there is a new version of jdom try to download it and install it in your project directory.

Java trying to find 3rd party class in my package even though that class is in an included JAR

I'm new to Java and NetBeans and I'm having a very hard time getting a simple project started.
I'm trying to include .jar files I need to work on a NetBeans plugin. I can successfully add the .jar files to my project using a variety of attempted methods.
I added .jar files by the project properties and added the 'wrapped jar' files to the project
I added the .jar files to the 'Libraries' item in the Projects explorer tree
Both methods appear to work in the IDE. They allow the desired classes to be accessed in the IDE and no syntax or access errors (etc) are detected. However, when I build and run I get ten pages of errors such as NullPointerException and this doozy:
java.lang.ClassNotFoundException: Will not load class org.netbeans.modules.openide.nodes.NodesRegistrationSupport arbitrarily from one of ModuleCL#2afef4c1[org.openide.nodes] and ModuleCL#2debe24[com.myproject.simplelauncherbutton] starting from SystemClassLoader[316 modules]
com.myproject.simplelauncherbutton is my own package. Why would NetBeans even be looking in my package for this class? And even if it is looking there, how can it be finding the class there, to be confused? I just want to make a NetBeans plugin using a .jar file for support. How can I get this working?!
Solved with help from a coworker. The problem was the way in which I was including packages in my project. I was trying to include packages in my project that did not seem to be available. For example, I needed to use org.openide.nodes, so at the top of my class file I wrote:
import org.openide.nodes;
NetBeans would respond saying it couldn't find this package. When I found a wrapped JAR package containing org.openide.nodes and included that in my project, it generated a slew of errors too long to list here.
However, when I add the module by its English name "Nodes API" in the project properties, everything works fine. I wish there were some documentation or instructions I had been able to find to save me hours of stressing about why I couldn't get NetBeans to recognize the various versions of org.openide.* I was trying to use.

Import existing .java files into IntelliJ?

I'm trying to import .java files into a new IntelliJ project and am really not having any luck (which includes Google, Stack, IntelliJ docs) finding out how to do so.
I create a new project, the Java SDK is configured correctly and everything works fine. The Main class of the IntelliJ project is located in a package called 'com.company', which is located in a 'src' directory. I try adding my files to the package, no go. I try dumping my files into the 'src' directory, can't instantiate any objects.
I've tried import statements to no avail.
When I try to instantiate an object of any class, a red line appears under the class name with 'Cannot resolve myClass'. myClass = all of my classes
Can somebody tell me what the missing piece is?

Already imported the jar files in netbeans but my source cannot find the library

I already imported the jar files in netbeans but my main java file still cannot find the methods. I am using netbeans 7.3.1 on Mac. Does anyone know how make it work?
Thank you,
I also had problem with importing classes from imported jar. It was so damn annoying...
There are couple solutions # web, (clear cache in user's AppData. or make sure to add jar's properly - by using "Add Jar/Folder..." option) but none of them worked for me.
What worked was new project and importing THE SAME (that's sad) jar files into it. So I had 2 project with exactly the same contents, but only one of them was working (compiling).
EDIT:
However, your problem is not related to mine. Root cause of your issue is the jar itself.
It contains class files in default package that is making them impossible to import.
Read here how to import class from default (unnamed) package.
hey guyes just keep your .jar files in the "C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext" it will work definitely.I have tried all the other options but i finally copied my .jar files in above path and it worked.

Categories