IntelliJ: Classes from imported jar not visible inside packages - java

I added external jar file(stdlib.jar on picture) to my project in IntelliJ(12.1.4), but when I want to use some static methods from it in created package(ChapterOne) it can't resolve it. However it finds classes in default package without problem. How I can make it work ?
It seems that dependencies are ok.

The libraries were configured correctly in IDEA, but the jars actually contained sources, not classes. So the solution is to unpack these sources from the jar files into the module src directory.

Related

Eclipse doesn't see classes from target/generated-sources

I have a solution with multiple projects(maven parent pom and a couple of child maven projects). Some of the projects are just a maven project that generates classes from wsdl or xsd using xjc-schema maven plugin.
The plugin generates the classes in target/generated-sources folder.
Now the other projects in the solution has to use the generated classes but eclipse doesn't recognize the generated classes.
Intellij Idea has no problem with that, it can recognize the generated classes, but I want to use Eclipse.
I tried that with Eclipse Neon and Spring Tool Suite based on Neon.
I tried to refresh, restart etc. with no success.
Also tried to add the project with the generated classes to Java Build Path -> Projects of the project that has to use those classes.
you should make the folder of the generated classes to a source folder.
Also make sure the output folder is correctly set on the java build path. If Contains Test Sources is set to Yes, the generated classes will not be detectable from your main classes.
In case that you are already seeing target/generated-sources/... as a source folder in Eclipse but the IDE is still unaware of the generated classes, go to Configure Build Path... and check that there is no Exclusion Pattern set. This was causing the problem in my case.
I had a problem with the setting the target/generated-sources as source folder. I had a subfolder with generated classes, and but target/generated-sources set as source folder. Eclipse didn't read the subfolder, though. I removed the target/generated-sources and added the target/generated-sources/<subfolder> and it worked.

Importing classes from the root of external jars (.jar) in Intellij (Maven Project)

I'm using IntelliJ 2016 and I have some Maven dependencies (for ImageJ plugin development) that I added successfully, the two external libraries show up in the form of jar files . I've added them in project structure -> module -> dependencies and they show up correctly under external libraries as well, the classes show up inside them, and the dependency scope is set to compile.
One of them is being imported successfully, while the other is not.
The one that works is in a sub folder in the jar
import fiji.threshold.Auto_Local_Threshold;
the other one is right under the jar and the name is not recognized when I try:
import MultipleKymograph_;
I can not change the path or declare a package for MultipleKymograph_
The dependencies are imported using POM and should be ok. I saw similar questions and I tried invalidating Intellij cache. my project is in a package that com.mycompany.imagej. I think the key here is that the specific classes that I can't access are directly in the jars.
enter image description here
Just to cross-reference for anyone else who stumbles across this: the issue is that the class MultipleKymograph_ was in the default package. You cannot import classes from the default package in other code. The solution was to move it to its own package; it now lives in sc.fiji.multiKymograph.

include jar file for only one package

I want to include a jar file which is used by only 1 package in my project.
|____package1
|____package2
|------------class1
|------------class2
|------------newjar
This is since I am trying to run Unit tests that needs a specific classes contained in this jar.
I do not want this jar for other packages, since I have edited this jar and created another jar for the rest of my project.
Not sure if this is allowed. Found a few articles that speak about including the jar in runtime but that is not what I want, alteast thats what I feel.
If you have the ability to edit the jar already, just rename one and include them both. There is no way (that I know of) to add a jar to the build path of only a single package.
If you are creating the jar from your own project then while creating the jar use the package/classes you want to be available in the jar.
If you are not creating the jar from your own project, let say you are using ODBC jar. In java build path, we include the complete jar which will be vailable in the classpath. So that we can import the resources when required. Hope this answer your questions.

Java/Eclipse: Export classes, interfaces AND REFERENCED LIBRARIES to a jar file

I wrote several java-classes and -interfaces.
These classes are using a referenced library.
Without the referenced library, the classes won't work.
I want to export these classes/interfaces to a jar-File. Not a runnable jar, because i want to use this jar-File as a library later.
But it seems, after creating my jar-file, that my referenced library is not included.
Does anyone have an idea, what the problem might be?
If you have a project which uses an external jar, in order to execute it you need the library's classes to be in your classpath.
You can achieve this through several ways:
When executing your jar, add to the classpath the path to the referenced library
Create a "lib" folder inside your project's structure and add it to your project. Inside Eclipse , right click on your project
Use maven to compile and execute your jar, with the maven-dependency-plugin and the maven-jar-plugin. It will copy your jars and include them in the manifest.mf before executing your jar.
Use maven-assembly-plugin to create a fat jar. It will open your referenced library jar and copy all the classes inside that library into your current project just before generating the jar. Thus, every class needed will be inside the jar.

How to prevent IntelliJ from including 3rd-party JAR inside my JAR?

How do I prevent IntelliJ from including 3rd-party JARs inside my JAR ?
It's insanely annoying. Basically I want one of these:
to produce jar with my code only. 3rd-party jar will be referenced in manifest.
OR
to produce jar with extracted classes of 3rd-party libraries.
The only problem is that IntelliJ produces JAR with other jar files inside. They are useless because java doesn't see them anyway when I run my jar via java -jar my.jar. I have to manually delete them and repack JAR/ZIP file.
Dependencies are not marked as "Export" in Settings.
I think jars end up in my jar because I added "compile output" in layout of my artifact. But I'm not sure how I can make compile output without jars of dependencies.
I tried setting Scope of dependencies to "Provided". It didn't help. They still get copied to output.
Thanks!
This behavior is controlled by the following options when you create a jar artifact in IDEA:
Refer to help for details.

Categories