Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use scala to debug Java programs

Tags:

java

scala

I was an interviewing a candidate for a Senior java position and he mentioned that they extensively use scala command prompt to debug Java programs. He said it was even possible to call a Java function directly from the scala command prompt.

I have never used scala so I was unable to verify his comments. It might be possible because both of them run on the JVM itself.

A quick google search doesn't give any pointers to this.

Is it really possible to debug Java functions from scala. Can someone give me pointers to the same.

Thanks in advance.

like image 214
Geek Avatar asked Feb 13 '23 22:02

Geek


1 Answers

Yes you can, though you would have to use scala syntax:

:: ~ » scala                                                                               
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val x = new java.io.File("/tmp/foo")
x: java.io.File = /tmp/foo

scala> x.length
res0: Long = 24777

Here I used scala REPL which is bundled with scala distribution, but in fact, you can use sbt (scala analog to maven) command so all project classes are instantly available on class path:

sbt
console
.... // using java code, just like any other jvm code

It is possible to use vanila REPL for the same purpose, but you will have to manually reconstruct classpath:

scala -classpath ...

And yes, as @Boris mentioned in the comments, IDEs (originally Eclipse, but now IDEA too) has it's own way of this feature -- build in, interactive worksheets (relevant plugins has to be installed):

enter image description here

like image 84
om-nom-nom Avatar answered Feb 15 '23 13:02

om-nom-nom