我在Scala中有一个应用程序,它使用一些用Java编写的库。在Build.scala
:
lazy val defaultSettings = Defaults.defaultSettings ++ buildSettings ++ Seq(
scalacOptions ++= Seq("-encoding", "UTF-8", "-optimise", "-deprecation", "-unchecked"),
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
parallelExecution in Test := false,
compileOrder := CompileOrder.JavaThenScala // Java then Scala
)
以下是我所拥有的简化版本。
当我在REPL(sbt
"project my-project" "console"
中运行以下代码时 - 我的项目中有一些模块,这就是我使用"project my-project"
)并打印“public class JavaClass1 extends javax.xml.ws.Service....
”的原因然后“val res1 = new JavaClass1() .... res1.getResult()
”它运作良好,没有错误。
我仔细研究了这一点,并且只有当我将应用程序作为普通应用程序运行时才会发生错误 - "sbt" "project my-project" "run"
:
// my app in Scala
object Main extends App {
val res1 = new JavaClass1() // error
res1.getResult()
}
// a class in a different library written in Java
public class JavaClass1 extends javax.xml.ws.Service
{
private final static URL WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(JavaClass1.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = JavaClass1.class.getResource(".");
url = new URL(baseUrl, "blabla.wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed");
logger.warning(e.getMessage());
}
WSDL_LOCATION = url;
}
public JavaClass1() {
super(WSDL_LOCATION, new QName("someDefaultURI", "someName")); // here is the error caused
}
//........
}
错误为last test/compile:run
:
[info] Running myApp.Main
[debug] Starting sandboxed run...
[debug] Waiting for threads to exit or System.exit to be called.
[debug] Classpath:
[debug] /home/me/projects/test-project/test/target/scala-2.10/classes
[debug] /home/me/projects/test-project/common/target/scala-2.10/classes
[debug] /home/me/projects/test-project/project1/target/scala-2.10/classes
[debug] /home/me/projects/test-project/project2/target/scala-2.10/classes
[debug] /home/me/projects/test-project/project3/target/scala-2.10/classes
[debug] /home/me/projects/test-project/./lib/lib123-ws-1.5-javadoc.jar
[debug] /home/me/projects/test-project/./lib/lib123-ws-1.5-sources.jar
[debug] /home/me/projects/test-project/./lib/lib123-ws-1.5.jar
[debug] /home/me/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.10.3.jar
[debug] /home/me/.ivy2/cache/com.typesafe.akka/akka-actor_2.10/jars/akka-actor_2.10-2.2.1.jar
[debug] /home/me/.ivy2/cache/com.typesafe/config/bundles/config-1.0.2.jar
[debug] /home/me/.ivy2/cache/org.projectlombok/lombok/jars/lombok-0.11.4.jar
[debug] /home/me/.ivy2/cache/com.wagnerandade/coollection/jars/coollection-0.2.0.jar
[debug] /home/me/.ivy2/cache/org.javatuples/javatuples/jars/javatuples-1.1.jar
[debug] /home/me/.ivy2/cache/net.databinder.dispatch/dispatch-core_2.10/jars/dispatch-core_2.10-0.11.0.jar
[debug] /home/me/.ivy2/cache/com.ning/async-http-client/jars/async-http-client-1.7.16.jar
[debug] /home/me/.ivy2/cache/io.netty/netty/bundles/netty-3.6.3.Final.jar
[debug] /home/me/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.6.2.jar
[debug] /home/me/.ivy2/cache/org.json4s/json4s-native_2.10/jars/json4s-native_2.10-3.2.5.jar
[debug] /home/me/.ivy2/cache/org.json4s/json4s-core_2.10/jars/json4s-core_2.10-3.2.5.jar
[debug] /home/me/.ivy2/cache/org.json4s/json4s-ast_2.10/jars/json4s-ast_2.10-3.2.5.jar
[debug] /home/me/.ivy2/cache/com.thoughtworks.paranamer/paranamer/jars/paranamer-2.5.6.jar
[debug] /home/me/.ivy2/cache/org.mockito/mockito-all/jars/mockito-all-1.9.0.jar
[debug] /home/me/.ivy2/cache/org.scala-lang/scalap/jars/scalap-2.10.0.jar
[debug] /home/me/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.10.0.jar
[debug] /home/me/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.0.jar
[debug] /home/me/.ivy2/cache/org.apache.httpcomponents/httpclient/jars/httpclient-4.2.5.jar
[debug] /home/me/.ivy2/cache/org.apache.httpcomponents/httpcore/jars/httpcore-4.2.4.jar
[debug] /home/me/.ivy2/cache/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar
[debug] /home/me/.ivy2/cache/commons-codec/commons-codec/jars/commons-codec-1.6.jar
[debug] /home/me/.ivy2/cache/org.scalaj/scalaj-http_2.10/jars/scalaj-http_2.10-0.3.10.jar
[debug] /home/me/.ivy2/cache/com.fasterxml.jackson.core/jackson-databind/jars/jackson-databind-2.2.2.jar
[debug] /home/me/.ivy2/cache/com.fasterxml.jackson.core/jackson-annotations/jars/jackson-annotations-2.2.2.jar
[debug] /home/me/.ivy2/cache/com.fasterxml.jackson.core/jackson-core/jars/jackson-core-2.2.2.jar
[debug] /home/me/.ivy2/cache/scala_2.10/sbt_0.13/com.github.mpeltonen/sbt-idea/jars/sbt-idea-1.5.1.jar
[debug] /home/me/.ivy2/cache/commons-io/commons-io/jars/commons-io-2.0.1.jar
[debug] /home/me/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.sbteclipse/sbteclipse-plugin/jars/sbteclipse-plugin-2.4.0.jar
[debug] /home/me/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.sbteclipse/sbteclipse-core/jars/sbteclipse-core-2.4.0.jar
[debug] /home/me/.ivy2/cache/org.scalaz/scalaz-core_2.10/bundles/scalaz-core_2.10-7.0.2.jar
[debug] /home/me/.ivy2/cache/org.scalaz/scalaz-effect_2.10/bundles/scalaz-effect_2.10-7.0.2.jar
[debug] Waiting for thread run-main to exit
[error] (run-main) java.lang.NoClassDefFoundError: Could not initialize class com.sun.xml.internal.ws.api.BindingID
java.lang.NoClassDefFoundError: Could not initialize class com.sun.xml.internal.ws.api.BindingID
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseBinding(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
at javax.xml.ws.Service.<init>(Unknown Source)
at JavaClass1.<init>(JavaClass1.java:46)
at myApp.Main$delayedInit$body.apply(Main.scala:26)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at myApp.Main$.main(Main.scala:13)
at myApp.Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sbt.Run.invokeMain(Run.scala:72)
at sbt.Run.run0(Run.scala:65)
at sbt.Run.sbt$Run$$execute$1(Run.scala:54)
at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:58)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
[debug] Thread run-main exited.
[debug] Interrupting remaining threads (should be all daemons).
[debug] Sandboxed run complete..
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1628)
at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1628)
at scala.Option.foreach(Option.scala:236)
at sbt.BuildCommon$class.toError(Defaults.scala:1628)
at sbt.Defaults$.toError(Defaults.scala:34)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$36$$anonfun$apply$37.apply(Defaults.scala:647)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$36$$anonfun$apply$37.apply(Defaults.scala:645)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
at sbt.std.Transform$$anon$4.work(System.scala:64)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
at sbt.Execute.work(Execute.scala:244)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
[error] (test/compile:run) Nonzero exit code: 1
更新:
在REPL中:
scala> object Main extends App {
| val res1 = new JavaClass1()
| }
defined module Main
scala> Main.main(Array[String]())
java.lang.AbstractMethodError: Main$.scala$App$_setter_$executionStart_$eq(J)V
at scala.App$class.$init$(App.scala:42)
at Main$.<init>(<console>:20)
at Main$.<clinit>(<console>)
at .<init>(<console>:22)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:900)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
scala>
UPDATE2:
有时会出现其他错误:
[error] (run-main) java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at javax.xml.soap.FactoryFinder.newInstance(Unknown Source)
at javax.xml.soap.FactoryFinder.find(Unknown Source)
at javax.xml.soap.FactoryFinder.find(Unknown Source)
at javax.xml.soap.SAAJMetaFactory.getInstance(Unknown Source)
at javax.xml.soap.MessageFactory.newInstance(Unknown Source)
at com.sun.xml.internal.ws.api.SOAPVersion.<init>(Unknown Source)
at com.sun.xml.internal.ws.api.SOAPVersion.<clinit>(Unknown Source)
at com.sun.xml.internal.ws.api.BindingID.<clinit>(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseBinding(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
at javax.xml.ws.Service.<init>(Unknown Source)
at JavaClass1.<init>(JavaClass1.java:46)
at myApp.Main$.main(Main.scala:14)
at myApp.Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sbt.Run.invokeMain(Run.scala:72)
at sbt.Run.run0(Run.scala:65)
at sbt.Run.sbt$Run$$execute$1(Run.scala:54)
at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:58)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
Caused by: java.util.MissingResourceException: Can't find com.sun.xml.internal.messaging.saaj.soap.LocalStrings bundle
at java.util.logging.Logger.setupResourceInfo(Unknown Source)
at java.util.logging.Logger.<init>(Unknown Source)
at java.util.logging.Logger.<init>(Unknown Source)
at java.util.logging.LogManager$SystemLoggerContext.demandLogger(Unknown Source)
at java.util.logging.LogManager.demandSystemLogger(Unknown Source)
at java.util.logging.Logger.demandLogger(Unknown Source)
at java.util.logging.Logger.getLogger(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SAAJMetaFactoryImpl.<clinit>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at javax.xml.soap.FactoryFinder.newInstance(Unknown Source)
at javax.xml.soap.FactoryFinder.find(Unknown Source)
at javax.xml.soap.FactoryFinder.find(Unknown Source)
at javax.xml.soap.SAAJMetaFactory.getInstance(Unknown Source)
at javax.xml.soap.MessageFactory.newInstance(Unknown Source)
at com.sun.xml.internal.ws.api.SOAPVersion.<init>(Unknown Source)
at com.sun.xml.internal.ws.api.SOAPVersion.<clinit>(Unknown Source)
at com.sun.xml.internal.ws.api.BindingID.<clinit>(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseBinding(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
at javax.xml.ws.Service.<init>(Unknown Source)
at JavaClass1.<init>(JavaClass1.java:46)
at myApp.Main$.main(Main.scala:14)
at myApp.Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sbt.Run.invokeMain(Run.scala:72)
at sbt.Run.run0(Run.scala:65)
at sbt.Run.sbt$Run$$execute$1(Run.scala:54)
at sbt.Run$$anonfun$run$1.apply$mcV$sp(Run.scala:58)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
[debug] Thread run-main exited.
[debug] Interrupting remaining threads (should be all daemons).
[debug] Sandboxed run complete..
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1628)
at sbt.BuildCommon$$anonfun$toError$1.apply(Defaults.scala:1628)
at scala.Option.foreach(Option.scala:236)
at sbt.BuildCommon$class.toError(Defaults.scala:1628)
at sbt.Defaults$.toError(Defaults.scala:34)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$36$$anonfun$apply$37.apply(Defaults.scala:647)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$36$$anonfun$apply$37.apply(Defaults.scala:645)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
at sbt.std.Transform$$anon$4.work(System.scala:64)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
at sbt.Execute.work(Execute.scala:244)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
[error] (test/compile:run) Nonzero exit code: 1