Scala 即使添加了依赖项,如何修复ClassNotFoundException org.apache.commons.codec.binary.Base64?

Scala 即使添加了依赖项,如何修复ClassNotFoundException org.apache.commons.codec.binary.Base64?,scala,intellij-idea,sbt,Scala,Intellij Idea,Sbt,当我在Scala Sbt项目中工作时,我正在尝试从IntelliJ上的一个文件加密和解密密码。这是我的scala类: import java.security.MessageDigest import java.util import javax.crypto.Cipher import javax.crypto.spec.SecretKeySpec import org.apache.commons.codec.binary.Base64 class DataEncrypt {

当我在Scala Sbt项目中工作时,我正在尝试从IntelliJ上的一个文件加密和解密密码。这是我的scala类:

import java.security.MessageDigest
import java.util

import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64

class DataEncrypt {

    def encrypt(key: String, value: String): String = {
        val cipher: Cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
        cipher.init(Cipher.ENCRYPT_MODE, keyToSpec(key))
        Base64.encodeBase64String(cipher.doFinal(value.getBytes("UTF-8")))
    }

    def decrypt(key: String, encryptedValue: String): String = {
        val cipher: Cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING")
        cipher.init(Cipher.DECRYPT_MODE, keyToSpec(key))
        new String(cipher.doFinal(Base64.decodeBase64(encryptedValue)))
    }

    def keyToSpec(key: String): SecretKeySpec = {
        var keyBytes: Array[Byte] = (SALT + key).getBytes("UTF-8")
        val sha: MessageDigest = MessageDigest.getInstance("SHA-1")
        keyBytes = sha.digest(keyBytes)
        keyBytes = util.Arrays.copyOf(keyBytes, 16)
        new SecretKeySpec(keyBytes, "AES")
    }

    private val SALT: String = "jMhKlOuJnM34G6NHkqo9V010GhLAqOpF0BePojHgh1HgNg8^72k"

}
我的build.sbt文件:

name := "AnalyzeTables"

version := "0.1"

scalaVersion := "2.11.8"

// https://mvnrepository.com/artifact/org.postgresql/postgresql
libraryDependencies += "org.postgresql" % "postgresql" % "42.1.4"

// https://mvnrepository.com/artifact/commons-codec/commons-codec
libraryDependencies += "commons-codec" % "commons-codec" % "1.13"

// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.8.1"
我正在从项目创建一个jar文件,并通过运行以下命令在cli上执行它:

scala -cp /home/user/jars/postgresql-42.1.4.jar analyzetables_2.11-0.1.jar tablename
其中analyzetables_2.11-0.1.jar是我的jar文件,tablename是我在代码中读取的参数。如果我提交代码,我会看到类未找到异常:

java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.enc.DataEncrypt.decrypt(DataEncrypt.scala:21)
    at com.db.manager.GpConnection.<init>(GpConnection.scala:13)
    at com.gp.analyze.GetStats.run_analyze(GetStats.scala:21)
但他们都给出了
classNotFoundException
。我还尝试通过以下操作创建一个胖罐子:

project Structure->Artifacts-> + ->jars->from modules with dependencies and run the fat jar. 
在本例中,我看到NullPointerException

我的项目结构:


如果我必须更改设置或添加丢失的jar文件,有人能告诉我吗?

您必须将其放在类路径上,或者制作一个胖jar,或者将其添加到-cp a.jar:b.jar,或者-cp a.jar;b、 jar在一个不幸的OS.Sample
addSbtPlugin(“com.eed3si9n”%“sbt assembly”%“0.14.8”)
我把这个addSbtPlugin(“com.eed3si9n”%“sbt assembly”%“0.14.8”)放在build.sbt文件中?我没有assembly.sbt文件。@som snytt提到的是sbt plugin
sbt assembly
——它允许构建胖jar,它将把所有依赖项放在一个jar中。请查看GitHub项目以了解更多详细信息。
project Structure->Artifacts-> + ->jars->from modules with dependencies and run the fat jar.