简体   繁体   中英

Convert java class to jar and use it as external library

I'm currently working on a security encryption algorithms. I found a java implementation I need, so to not invent a wheel again I want to convert the algorithm class to jar and then import it as a external library in my project.

I've tried:

jar -cvf result.jar class.class

It makes a .jar file but once I try to use it in Intellij File>Project Structure>Libraries>+ and after use it, it doesn't get recognized in code with it's given name ( local.java.algorithm ).

To wrap up I want to do is: simple java class > jar > add to project as external library > use it in code

How to make this jar recognizable by IDEA?

When you create a JAR file containing classes, the structure of the JAR file needs to mirror the package name structure.

For example, if you have a class called MyClass.java whose contents looks like this:

package local.java.algorithm;
// imports
public class MyClass ... {
}

You compile that to MyClass.class in the directory local/java/algorithm. The structure of the JAR file then needs to be:

/
/local
/local/java
/local/java/algorithm
/local/java/algorithm/MyClass.class

I suspect that you have actually created it like this:

/
/MyClass.class

which will mean that Intellij (or javac or anything else) won't be able to resolve

import local.java.algorithm.MyClass;

at compile time OR at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM