我正在编写一个scala类,它会进行休息调用以上传zip文件。我正在使用SBT从这个类中创建一个jar。 'sbt package'命令给了我一个罐子。我已经在lib文件夹中添加了所有依赖项jar(httpcore-4.3,httpclient-4.5.1,httpmime-4.5.1)。 sbt运行正常,我得到正确的输出。但是当我运行jar时,我得到以下异常。
java.lang.ClassNotFoundException: org.apache.http.entity.mime.content.ContentBody
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at restTesting.main(restTesting.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:71)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:139)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:71)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:139)
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
at scala.tools.nsc.JarRunner$.run(MainGenericRunner.scala:16)
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
at scala.tools.nsc.JarRunner$.runJar(MainGenericRunner.scala:28)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:78)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
这是我的Scala类
`
import java.io.File
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.mime.{HttpMultipartMode, MultipartEntityBuilder}
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.impl.client.HttpClientBuilder
object restTesting {
def restTest(fileName:String):Unit =
{
val url = "http://ip:port"
val post = new HttpPost(url)
post.addHeader("Content-Type","application/zip")
post.addHeader("zipFileName","zipdemocode2")
val bin = new FileBody(new File(fileName))
val reqentity = MultipartEntityBuilder.create()
reqentity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
reqentity.addPart("bin",bin)
val entity = reqentity.build()
post.setEntity(entity)
val client = HttpClientBuilder.create().build()
val response = client.execute(post)
val resentity = response.getEntity
println(resentity.getContent.read())
}
def main(args:Array[String])
{
restTest("path to zip file")
}
}
`