通常,我在项目中使用 maven,但由于一些迁移问题,我必须(暂时)下载 jar。
我想使用以下代码:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;
public class Main {
public static void main(String[] args) {
ByteArrayOutputStream out = QRCode.from("Hello World")
.to(ImageType.PNG).stream();
try {
FileOutputStream fout = new FileOutputStream(new File(
"C:\\QR_Code.JPG"));
fout.write(out.toByteArray());
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// Do Logging
} catch (IOException e) {
// Do Logging
}
}
}
我在我的项目中添加了一个 jar:
qrgen-1.3.jar
但是,我遇到了一个异常(exception):
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/zxing/Writer
所以我添加了另外两个 jar :
zxing-core-1.7.jar
zxing-j2se-1.7.jar
现在,我遇到了另一个错误:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.zxing.Writer.encode(Ljava/lang/String;Lcom/google/zxing/BarcodeFormat;IILjava/util/Map;)Lcom/google/zxing/common/BitMatrix;
这里我无法修复它。
哪里可能有问题?
我确信它从我的代码的第一行开始:
ByteArrayOutputStream out = QRCode.from("Hello World").to(ImageType.PNG).stream();
最佳答案
好吧,您正在使用此库以及错误版本的 zxing。看起来 1.3 使用 2.0,而您将其与 1.7 配对:https://github.com/kenglxn/QRGen/blob/e74f7912e19eb99c84100d5840e2be2e48108747/pom.xml#L40
这几乎总是此类错误的含义。使用像 Maven 这样的工具可以避免这种情况。此外,这些依赖项的两个版本现在都已过时。
关于java - qrgen 和 zxing 库出现 java.lang.NoSuchMethodError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22781101/