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.

4
  • 1
    Write it in plain java instead. Commented Sep 15, 2021 at 16:24
  • hostname doesn't have to be installed, it's present by default.
    – Gaël J
    Commented Sep 15, 2021 at 21:41
  • Also Scala compiles to JVM bytecode, as you said no need to have scala installed on the machine to run scala code.
    – Gaël J
    Commented Sep 15, 2021 at 21:43
  • Agree with @ThorbjørnRavnAndersen it will be faster to write it in Java, what is the purpose to have it in Scala on your machine? Commented Sep 16, 2021 at 9:39

0

Browse other questions tagged or ask your own question.