简体   繁体   中英

Why can I start the Scala compiler with “java -cp scala-library.jar;. Hello World”?

I do understand that scala is built on top of java and that you can write java code in scala and execute it. But when I was looking through the source code of scala I could not find any java file. I would have understood if the compiler was built in Java and you can use now a syntax which has no similarities to Java. But I could not find any.

So why can I use the java compiler to run scala programms?

You can't literally write java code in scala if you meant you could use java syntax in .scala files, but scalac is able to recognise .java files and opt to javac for their compilation. Most of the jvm languages, like java, scala, groovy, clojure and so on are compiled down to the same bytecode, so JVM simply doesn't know if code was written in one language or another.

why can I use the java compiler to run scala programms

You can't. Java compiler is just a translator from java language to java bytecode. The point is that java virtual machine is only able to run bytecode (common language, looks similar to assembly) and scala is translated to this universal bytecode (class files on the picture below which is taken from there ). But at the same time, scala program usually requires some dependencies (eg scala collections library and a whole slew of different apis) so you have to put scala-library jar on the classpath).

在此处输入图片说明

Short version: you're not using the java compiler, you're using the java runtime.

Long version: there's a big difference between javac and java . javac is the java compiler, which takes in java source code and outputs jvm bytecode. java is the java runtime, which takes in jvm bytecode and runs it.

But one of the great things about the jvm is that you can generate bytecode for it any which way. Scala generates jvm bytecode without any java source code.

The scala library assumedly has a META-INF/MANIFEST.MF with a Main-Class attribute. That (jvm) main class is scala running the scala singleton.

Well, presumably the very first Scala compilers were in fact written in Java. But, once the first basic Scala compiler had been created, subsequent versions of the compiler could be written 100% in Scala, using the earlier Scala compiler to build them.

It's a process called 'bootstrapping'. The same methodology explains why gcc could be written entirely in C (I'm not saying it is, but, it could be).

There is a fascinating article by Ken Thompson called "Reflections on Trusting Trust", which explains how it's possible to hack a C compiler (or any compiler) permanently in a way that is, for all practical purposes, undetectable, even if you have the source code:

http://dl.acm.org/citation.cfm?doid=358198.358210

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