简体   繁体   中英

how to run scala jar file within java file?

I need to run Scala jar file from Java code:

So, if I had this Scala code:

object test extends App {
    override def main(args: Array[String]) {
        println("Hello, world! " + args.toList)
    }
}

and I exported in demo.jar , I want to execute from inside Java application? The way I'm using it is to throw runtime process, which not working with me?

import java.io.InputStream;
import org.apache.commons.io.IOUtils;

public class testscalajar {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"scala","-cp","d:\\demo.jar", "test"});
            // Then retrieve the process output
            InputStream in = proc.getInputStream();
            InputStream err = proc.getErrorStream();
            String inString = IOUtils.toString(in, "UTF8"); 
            String errString = IOUtils.toString(err, "UTF8"); 
            System.out.println(inString);
            System.out.println(errString);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Anybody have any other solutions?????

Scala compiles down to ordinary JVM .class files, you don't need any external processes to use Scala code from Java or Java code from Scala.

Make sure that your path settings are correct, then simply create Scala objects and call Scala methods from your Java code as if it were just yet another Java library.

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