简体   繁体   中英

How can I use the Scala REPL to test java code - java and scala REPL giving different outputs

I am trying to use the REPL for testing my java code. I had set my class path and launched the REPL and I could access my application classes inside REPL.

I launched a method which makes a webservice call and it is throwing a java.lang.ClassCastException. I wrote a java program which does the same thing as the series of commands entered in the REPL and ran it with the same classpath, it is running without issues and giving the result.

Any clues on why it is not running under scala? Does the rules of casting is strict in scala?, and if I am using an API on which I have no control how can I tell Scala to be behave like java so, that I can test my methods which makes a lot of API calls and code written by others.

java.lang.ClassCastException: weblogic.webservice.encoding.ImageCodec cannot be cast to weblogic.xml.schema.binding.Serializer
        at weblogic.xml.schema.binding.internal.XSDTypeMapping.addOptionalEntries(XSDTypeMapping.java:762)
        at weblogic.xml.schema.binding.internal.XSDTypeMapping.createOptionalMapping(XSDTypeMapping.java:665)
        at weblogic.xml.schema.binding.internal.XSDTypeMapping.createPrimaryMapping(XSDTypeMapping.java:655)
        at weblogic.xml.schema.binding.internal.XSDTypeMapping.createInstance(XSDTypeMapping.java:138)
        at weblogic.xml.schema.binding.internal.XSDTypeMapping.<clinit>(XSDTypeMapping.java:118)
        at weblogic.xml.schema.binding.internal.TypeMappingFactoryBase.createDefaultMapping(TypeMappingFactoryBase.java:18)
        at weblogic.webservice.core.encoding.DefaultRegistry.<init>(DefaultRegistry.java:110)
        at weblogic.webservice.core.encoding.DefaultRegistry.<init>(DefaultRegistry.java:37)
        at weblogic.webservice.core.rpc.ServiceImpl.<init>(ServiceImpl.java:91)

Since many are asking for the source code to answer this, here is the snippet I entered in the scala REPL. It makes API calls which are copyrighted and cannot be shared here, I can answer to questions to provide more info on this issue without violating the norms.

val clientManager=new ClientManager()
val client=clientManager.getClient(<clientId>)
/*client location is stored in another system which exposes its data as a webservice.
 We use weblogic tools to compile the WSDL to create client (Web service client, not to
 be confused with the client object which represent a customer) to access the system
 hosting the data*/
val res=client.getLocation //exception is thrown at this line where a web-service call is made

I did something different and I got a completely different exception this time instead of using val res=client.getLocation I used client.getLocation without assigning it to a variable and this time the exception thrown is

java.lang.NoClassDefFoundError: Could not initialize class weblogic.xml.schema.binding.internal.XSDTypeMapping
        at weblogic.xml.schema.binding.internal.TypeMappingFactoryBase.createDefaultMapping(TypeMappingFactoryBase.java:18)
        at weblogic.webservice.core.encoding.DefaultRegistry.<init>(DefaultRegistry.java:110)
        at weblogic.webservice.core.encoding.DefaultRegistry.<init>(DefaultRegistry.java:37)
        at weblogic.webservice.core.rpc.ServiceImpl.<init>(ServiceImpl.java:91)

I ran the interpreted with the -nobootcp option and it ran without throwing any exception which are above but the result I got is null in both the cases

Anyone using Scala REPL to test client written from stubs generated by weblogic 10.3 and were successful in using it?

Your stack trace has nothing to do with Scala. The exception is happening deep in some weblogic code. Why do you think Scala is causing the problem? The most likely cause is a mismatch in library, eg you're using a different class path in your Scala test than in your Java test.

Still, I'll try to answer your implicit question: for reference types that can be expressed equivalently in both Java and Scala, Scala's rules for casting are the same as Java's.

Without the code it's really hard to tell what's going on. However... since you mentioned that you are starting a web service I'm guessing you are using Scala remote actors.

Before you start the actor try doing classLoader = getClass().getClassLoader() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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