Idea Intellij: Can't import the libraries: package does not exist - java

I'm trying to make use of a jReddit library, which, in turn requires apache HttpComponents and Commons IO libraries.
I downloaded the sources, added them in Itellij Idea through File - Project Structure - Modules - Add Content Root.
All the classes from the libraries that my code makes use of are imported successfully. But the problem appears when compiling - it says that package com.github.jreddit.oauth does not exist and package org.apache.http.impl.client does not exist and that it cannot find symbol of those libraries' classes.
Why does this happen and how to fix it?

Don't use Maven or Gradle if you can't even manage adding a JAR to your project manually.
You should acquire those JARs (containing .class byte code files, not .java source), add them a folder in your project named /lib, and add that directory as a JAR source location in your project. They'll be in the CLASSPATH then.
You need to add the /lib folder to an artifact when you run. Be sure you know how to do that as well.

I ran into this error after upgrading IntelliJ to version 2019.1. These steps fixed it for me:
Click Run from the toolbar
Choose Edit Configurations
Make sure the Scratch file you want to run is selected on the left panel
In Use classpath of module dropdown, select the project module that contains the proper module

Related

Getting NoClassDefFoundError when using common.lang.StringUtils in android java code?

I am getting this error:-NoClassDefFoundError wherever the code is using StringUtils class or methods .I have correctly added the jar files to the code and is not showing any kind of error in my code(Using Eclipse Indigo).
Is there any kind of point I am missing out. I have used this jar file in other projects but they worked properly.
Please help If any one is getting similar kind of problem?Thanks
Yes, this because of your .jar file didn't import properly. Follow below steps -
Place your .jar file in your project's libs folder .
Import it into your project. And, GoTo project -> properties -> Java build path -> order tab.
Check, whether your .jar file checked and placed in order of 1st. This is the main thing.
Hope these steps helps you. Have a look at below image -
Note that the build classpath is different from the runtime classpath. You must add the required JARs to both.
It's one of the things I don't like in Eclipse...
EDIT: To add JARs to the runtime classpath: in Eclipse, double click the plugin.xml file associated with your project. On the Runtime tab there is a Classpath category. From there, you can add JARs to the runtime classpath.
Here is a screenshot to illustrate this:
Are the jar files in a lib folder or a libs folder?
Newer SDKs require the jar files to be in a libs folder.
I do not know how you execute your code, but it clearly means that the jar is not properly added to your classpath, especially if your code compiles in Eclipse.
Add it to your classpath -or re-check it is well added- when executing and it should work.
Right click on project, Select Build Path -> Configure Build Path and Select Libraries tab. Then with add Jars, add the jar in question.(if you are using eclipse)

NoClassDefFoundError on external library project for Android

I use eclipse for Google Android development.
I've created a library project ([x] Is Library in the Android-settings), which includes an external jar-file (Referenced Libraries). This library project are referenced in another Project (the actual project which will use the library project). This is done by add the project under the Android-settings.
the source compiles but if I want to execute it on the device, I get the NoClassDefFoundError for a class which is inside the jar-file which is included in the library project.
Edit: The jar-file ist added to the exported entries ([x] my.jar on the Order and Export-Tab from the library project)
Is there a clean way to get this working?
It has been clearly stated in offcial API here:
A library project can include a JAR library
You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file
The jar lib must be manually added to the dependent application project's build path, not only the library project build path itself.
Update from SDK r17:
This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:
Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)
For those who followed the steps(even check the projects in "Order and Export") and still have the java.lang.ClassNotFoundException in the API 17, the final step is to check that your compiler does not run with Java 1.7. If is 1.7 then you should change it to 1.6 for all your projects. After that it will ask to rebuild all the projects and successfully ran on my phone :)
To change the java compile version in eclipse, this is located in: Project properties > Java Compiler > Compiler Compliance level: 1.6
Go to project properties -> build path-> libraies
If you see your jar files like this
snmp4j.jar - e:\software\jars
Its may your problem
Add libs folder in your project and copy jar file in that folder. Right click jar file and go build path -> add to build path. Then you can see your jar as
snmp4j.jar - project_name/libs
Its worked for me.
I had two projects using the same library: one working, the other one crashing with java.lang.NoClassDefFoundError.
After nothing else helped me, I looked into the file project.properties in the root directory of my project.
The working project had the android.library.reference line (the last line below), the crashing one did not:
# Project target.
target=android-17
android.library.reference.1=../my-library-project
I manually added the line and it started working!
(Yes, I did try both (project) properties -- java build path -- projects and (project) properties -- java build path -- order and exports -- nothing helped.)
PS By the way, "project properties" has also the "project references" tab. No idea if it would work.
I had a minor issue when I upgraded to ADT17 where my libs weren't being imported properly. Turns out this is because my librarys were being linked as dependancies from my lib folder not libs!
Seems librarys have to be in the libs folder from now
I had a similar problem and non of the solutions out here fixed it.
Short version: the JAR was compiled in Java 1.7 and could not be found by Dalvik.
We build an internal JAR that we share across backend and mobile clients with Maven. The problem ended up being that the JAR was compiled with Java 1.7. Androids dalvik only supports Java 1.5 and 1.6. This is why everything works fine in Eclipse as it's not compiled to dalvik at this point.
We even specified the target and source version in the Maven compiler plugin to compile with Java 1.6. This did not work because only JDK 1.7 was installed on the build machine. A small note at the bottom of the Maven page gave us the hint: Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version.
To see if you have this problem as well, rename your *.jar file to *.zip unpack it, and look in the MANIFEST.MF file for the Build-Jdk: parameter to see what Java version actually compiled your JAR.
Another thing to pay attention to is library package names.
If you are using ADT21 and you happen to have libraries that have the same package name, there will be error during compile but it will still compile and run in Eclipse, giving you an APK that is missing some of the resource classes. Hence the crash when you run the app.
If you compile it with ANT then you can see the compile error that says two or more libraries use the same package name.
To fix this, rename your library project by using Android Tools -> Rename Application Package. Then everything will go back to normal.
It took me almost entire day to figure this out...

Adding additional java files to playframework classpath

I have a project that shares models with my android project. I have a separate eclipse project just for models and other shared code. I add this to my play project as a dependency in eclipse.
In eclipse, play compiles and starts without problem. However, I went to deploy to GAE and found that the compilation stage of play's packaging fails because it can't find the models.
I suspect I could hack the ant build files, but that seems brittle.
Is there a standard way to add extra directories to the play compilation source tree or the classpath?
Make a jar-file with your classes and put it in /lib. That's where I put my libraries.
Files in the application /lib folder is automatically added to the class path by Play Framework. See Classpath settings
From Anatomy: "The app directory contains all executable artifacts: Java and Scala source code, templates and compiled assets’ sources" ... and further: "You can of course add your own packages, for example an app/utils package"
You can copy java source files to make a hierarchy of packages under /app, e.g.:
/app/sharedcode/project2/models/domain1 and import that in WhateverController.scala as:
import sharedcode.project2.models.domain1._

How to use Java API's?

Probably really simple but I just cannot work out how to use any API's with Java (Using Eclipse).
Where do they go?
For example I want to use a Twitter API and I import it using:
import net.unto.twitter.Api;
I then include the API file in the same dictionary as my class file. (This is what I do in python)
Anyone care to tell me the simple answer?
Typically APIs are packaged in so-called JARs, which stands for Java ARchive. What you should do is:
Download the jar.
Put it in some sort of 'lib' directory in your project structure. Do not put it with your source code.
Add the jar to your classpath. Some IDEs you have to add jar by jar, others you tell the IDE which directories are library directories and it will modify the classpath for you to include all the jars in the directory.
Import the relevant package in your files where you use the API.
Once you have the jar added to your classpath and import the relevant classes, Eclipse should automatically pick it up and allow you to use autocomplete features against the API.
It is also a good idea to get the source jar for any libraries you download. Most IDEs let you attach src jars, so you can click into the API and see how the code is written.
You need to include any external JARs in the build path. In eclipse right click on the project and go to 'Build Path' > 'Configure Build Path' then go to the 'Libraries' tab and 'Add JARs'. Also, when using eclipse it is easier not to maintain the import statements yourself, but instead use 'Organize Imports' (ctrl + shft + o) or Import the selected object (ctrl + shft + m). If a matching object is available in the classpath it will be imported.
Java APIs are distributed in JAR files (which are just zip files with a specifiec directory structure).
Download the jar in question, put it in a known location on your disk (known location as in, pay attention to the name of the direcctory where you store the downloaded jar).
There are two times you will need to reference a Jar file:
During project compilation. This includes development of the project using an IDE like eclipse (they all compile the code to find errors).
Option 1, add an external jar:
Open the project properties in Eclipse
Select Java Build Path
Select the Libraries tab
Click the Add External Jar button
Browse to the location of the JAR in question.
Select the JAR.
Click the Open button.
Option 2, add the JAR to your web project's WEB-INF/lib directory.
During project execution. This will be after you deploy your application to a web container (perhaps tomcat). Store the JAR in question in the classpath of the appliction post deploy. For simple web applications, you can put the jar in the project WEB-INF/lib directory.
You should have it included in the classpath (build path) of the project.

eclipse library bundle - fails to export packages from some libraries

I've created a simple plugin project in eclipse 3.5 that just stores third-party libraries for the use by other bundles in an eclipse RCP application. Worked as expected: I edited the manifest, exported the required packages and added the libraries to the build path (project build path as well as manifest build path).
Some days later I added another jar to that project, did the same steps (exporting a package, adding the library to the build path(s)) but this time I can't import classes from that exported package in other bundles. The package was clearly selectable on the manifest editor but import statements in classes just taunt me with curly red lines. Importing classes from other packages exported by the bundles still works, only classes from the newly added lib remain invisible.
Has anyone has an idea what I might have missed? I'm pretty stuck and have no idea how to convince google to show me the solution to that ugly issue...
Check the following:
Make sure they are in the Build
Configuration -> Order and Export
area, and they are check for export.
Make sure you have the packages
exported in the manifest (PDE Tools
-> Open Manifest -> Runtime tab)
In the same place as above, make
sure the JAR files are in the
manifest classpath.
(as Al says
below), make sure the build
properties has your jars marked for
exclusion (though this is not likely
your issue as you are getting errors
compiling).
If all of this is done (and it's still not working), do a clean build an restart eclipse. Sometimes the Eclipse gets a little confused about this and a restart helps.
Check the build.properties to see if the bin.includes includes your newly added Jar. Without it, it won't be exported by the build process, and thus won't be able to use it in dependent bundles.

Categories