简体   繁体   English

将 java class 转换为 jar 并将其用作外部库

[英]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.我找到了我需要的 java 实现,所以为了不再发明轮子,我想将算法 class 转换为 jar,然后将其作为外部库导入我的项目中。

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 ).它会生成一个.jar文件,但是一旦我尝试在 Intellij File>Project Structure>Libraries>+中使用它,并且在使用它之后,它不会在具有给定名称的代码中被识别( 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总结一下我想做的是: simple java class > jar > add to project as external library > use it in code

How to make this jar recognizable by IDEA?如何使这个 jar 被 IDEA 识别?

When you create a JAR file containing classes, the structure of the JAR file needs to mirror the package name structure.创建包含类的 JAR 文件时,JAR 文件的结构需要镜像 package 名称结构。

For example, if you have a class called MyClass.java whose contents looks like this:例如,如果您有一个名为MyClass.java的 class,其内容如下所示:

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

You compile that to MyClass.class in the directory local/java/algorithm.您将其编译为目录 local/java/algorithm 中的 MyClass.class。 The structure of the JAR file then needs to be: JAR 文件的结构则需要为:

/
/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这将意味着 Intellij(或javac或其他任何东西)将无法解决

import local.java.algorithm.MyClass;

at compile time OR at runtime.在编译时或运行时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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