0

I want to perform this activity in Scala and execute it with Java:

hostname -i

The scala code for this is:

import java.net._

object HostName {
    def main(args: Array[String]){
        val localhost: InetAddress = InetAddress.getLocalHost
        val localIpAddress: String = localhost.getHostAddress
        println(localIpAddress)

    }
}

On the machine that would run the above program, there is no Scala installed. It does have Java installed though. From my understanding, Java can execute Scala code.

On my local, I perform these steps and get the answer:

scalac HostName.scala
scala HostName

I also noticed that there are two files generated:

HostName$.class
HostName.class

My objective is to run a Scala file with Java and get the result of hostname -i i.e. this code in shell. Something like this: OUT=$(<the command to execute the script and get the result of println>)

I have tried running the HostName with javap but it does not give the correct result:

JAVA_HOME=/opt/random_mde/package/RandomJDK11/current/Contents/Home/bin/javap HostName

Result is:

ab-mbp-parvals

instead of the IP address that comes from Scala code: a.b.c.d. If I run hostname and not hostname -i, I get ab-mbp-parvals. If I run hostname -i on linux machine, I get the IP.

How do I execute a scala code to perform the job of hostname -i without installing hostname on a machine that only has Java? I am happy to jar my code on a machine that has Scala installed.

Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81

0 Answers0