0

I have created an Android project and added an external JAR on IntelliJ. I have already installed "json-simple-1.1.jar" to use package org.jose4j.json.internal.json_simple. I did a library setting to use that from the project structure menu. But when I run the program, error message says, "error: package org.jose4j.json.internal.json_simple does not exist import org.jose4j.json.internal.json_simple.JSONArray;". (Not only JSONArray, but the other JSON such as JSONObject)

It worked when I used that outside of the Android project. Are there any special settings for the Android project?

So far, I stored the jar file under the project folder by making "lib" folder additionally.

mufugu
  • 13
  • 4
  • Is that a Gradle based project? Can you share the link to the jar file you are using and provide the source code that is referencing the classes from this jar? – CrazyCoder Mar 15 '20 at 02:20
  • Yes, it is a Gradle based project. In terms of jar file, I did not install anything from outside. I'm just using the library that IntelliJ has. According to that lib folder, it seems I am using jose4j-0.6.0.jar. – mufugu Mar 15 '20 at 02:24
  • This library is a different JSON implementation, it has this package: `org.json.simple`. But your code is using completely different JSON: `org.jose4j.json`. You either need to find the correct library or change your code. Also in the Gradle based projects all the dependencies must be defined in the `build.gradle` file, all the changes you make to the project structure will be discarded on reimport. If you need to import a local jar with Gradle, see this question and the answers: https://stackoverflow.com/questions/20700053/how-to-add-local-jar-file-dependency-to-build-gradle-file. – CrazyCoder Mar 15 '20 at 02:28
  • It might be a silly question, but when I made a simple java program without android such as client-server simple program, org.jose4j.json worked normally. So I thought my setting for the android project was wrong. Are there any reasons why it worked at a simple java program and it does not work at the android project? – mufugu Mar 15 '20 at 02:34
  • It's likely you are using different jars or different code. If you share your projects, I'll tell you what exactly the problem is. – CrazyCoder Mar 15 '20 at 02:35
  • yea. how can I share my project? there are may programs. Do I store the file on like Github? – mufugu Mar 15 '20 at 02:39
  • Yes, you can use GitHub, or just zip the folders and use any file sharing service (Dropbox, Google Drive, OneDrive, etc). – CrazyCoder Mar 15 '20 at 02:40
  • If you can not access the link, please let me know. I will use google drive. – mufugu Mar 15 '20 at 03:17

1 Answers1

0

In app/build.gradle dependencies section add the following:

compile group: 'org.bitbucket.b_c', name: 'jose4j', version: '0.7.0'

Reimport/refresh the project in IntelliJ IDEA.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • I added that, but still error happens. I saved the screenshot on the Github. – mufugu Mar 15 '20 at 03:45
  • It's another issue that is not what your question was about, but the reason is the same: missing libraries. Find the proper dependencies for these libraries and add them in build.gradle. Note that this line has no effect as the libs directory inside the app folder is empty: `implementation fileTree(dir: 'libs', include: ['*.jar'])`. If the issue with JSON is solved, feel free to accept the answer as it solves the original problem. – CrazyCoder Mar 15 '20 at 03:49
  • Thank you very much for your help!! In case of finding proper libraries, what is the best way to find? – mufugu Mar 15 '20 at 03:52
  • Search for the package name at https://mvnrepository.com/ and copy/paste the Gradle dependency line. – CrazyCoder Mar 15 '20 at 03:53
  • Thank you so much!! I really appreciate it! I did not know this operation in case of using jar file. I just did library setting from project structure. – mufugu Mar 15 '20 at 04:06
  • As described in the comments above, this approach will not work for Gradle and Maven projects, you must define the dependencies in the build files and IDE will import them. – CrazyCoder Mar 15 '20 at 04:07
  • Yes, I will. Thank you. If there is a good tutorial for this, would you mind giving me the link? I started the java recently, so there is a bunch of information on the web, I am not sure which one is the best to refer. – mufugu Mar 15 '20 at 04:11
  • Learn how to use Gradle, follow the official guides. – CrazyCoder Mar 15 '20 at 04:11
  • Please see https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – CrazyCoder Mar 15 '20 at 08:14
  • @mufugu If you won't accept valid answers, people will not be willing to help you here. It's the least you can do and is not so hard. As I can see from your profile you didn't accept a single answer yet. If there is anything I can improve in this answer, please let me know. – CrazyCoder Mar 24 '20 at 23:10