为什么我们需要其他JVM语言

时间:2023-01-24 22:25:30

I see here that there are a load of languages aside from Java that run on the JVM. I'm a bit confused about the whole concept of other languages running in the JVM. So:

我在这里看到除了Java之外还有大量语言在JVM上运行。我对JVM中运行的其他语言的整个概念感到有点困惑。所以:

What is the advantage in having other languages for the JVM?

使用JVM的其他语言有什么好处?

What is required (in high level terms) to write a language/compiler for the JVM?

为JVM编写语言/编译器需要什么(在高级术语中)?

How do you write/compile/run code in a language (other than Java) in the JVM?

如何用JVM中的一种语言(Java除外)编写/编译/运行代码?


EDIT: There were 3 follow up questions (originally comments) that were answered in the accepted answer. They are reprinted here for legibility:

编辑:在接受的答案中回答了3个跟进问题(最初的评论)。他们在这里重印了易读性:

How would an app written in, say, JPython, interact with a Java app?

如果用JPython编写的应用程序如何与Java应用程序交互?

Also, Can that JPython application use any of the JDK functions/objects??

此外,JPython应用程序可以使用任何JDK函数/对象吗?

What if it was Jaskell code, would the fact that it is a functional language not make it incompatible with the JDK?

如果它是Jaskell代码,那么它是一种函数式语言会不会使它与JDK不兼容?

17 个解决方案

#1


30  

To address your three questions separately:

分别解决您的三个问题:

What is the advantage in having other languages for the JVM?

使用JVM的其他语言有什么好处?

There are two factors here. (1) Why have a language other than Java for the JVM, and (2) why have another language run on the JVM, instead of a different runtime?

这里有两个因素。 (1)为什么JVM使用Java以外的语言,以及(2)为什么在JVM上运行另一种语言而不是运行不同的语言?

  1. Other languages can satisfy other needs. For example, Java has no built-in support for closures, a feature that is often very useful.
  2. 其他语言可以满足其他需求。例如,Java没有对闭包的内置支持,这一功能通常非常有用。

  3. A language that runs on the JVM is bytecode compatible with any other language that runs on the JVM, meaning that code written in one language can interact with a library written in another language.
  4. 在JVM上运行的语言与在JVM上运行的任何其他语言都是字节码兼容的,这意味着用一种语言编写的代码可以与用另一种语言编写的库交互。

What is required (in high level terms) to write a language/compiler for the JVM?

为JVM编写语言/编译器需要什么(在高级术语中)?

The JVM reads bytecode (.class) files to obtain the instructions it needs to perform. Thus any language that is to be run on the JVM needs to be compiled to bytecode adhering to the Sun specification. This process is similar to compiling to native code, except that instead of compiling to instructions understood by the CPU, the code is compiled to instructions that are interpreted by the JVM.

JVM读取字节码(.class)文件以获取它需要执行的指令。因此,任何要在JVM上运行的语言都需要编译为符合Sun规范的字节码。此过程类似于编译为本机代码,不同之处在于代码编译为由JVM解释的指令,而不是编译为CPU理解的指令。

How do you write/compile/run code in a language (other than Java) in the JVM?

如何用JVM中的一种语言(Java除外)编写/编译/运行代码?

Very much in the same way you write/compile/run code in Java. To get your feet wet, I'd recommend looking at Scala, which runs flawlessly on the JVM.

与在Java中编写/编译/运行代码的方式非常相似。为了让你的脚湿透,我建议你看一下在JVM上运行完美的Scala。

Answering your follow up questions:

回答你的后续问题:

How would an app written in, say, JPython, interact with a Java app?

如果用JPython编写的应用程序如何与Java应用程序交互?

This depends on the implementation's choice of bridging the language gap. In your example, Jython project has a straightforward means of doing this (see here):

这取决于实现选择弥合语言差距。在您的示例中,Jython项目有一个简单的方法来执行此操作(请参阅此处):

from java.net import URL
u = URL('http://jython.org')

Also, can that JPython application use any of the JDK functions/objects?

此外,JPython应用程序可以使用任何JDK函数/对象吗?

Yes, see above.

是的,见上文。

What if it was Jaskell code, would the fact that it is a functional language not make it incompatible with the JDK?

如果它是Jaskell代码,那么它是一种函数式语言会不会使它与JDK不兼容?

No. Scala (link above) for example implements functional features while maintaining compatibility with Java. For example:

例如,Scala(上面的链接)实现了功能特性,同时保持了与Java的兼容性。例如:

object Timer {
  def oncePerSecond(callback: () => unit) {
    while (true) { callback(); Thread sleep 1000 }
  }
  def timeFlies() {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies)
  }
}

#2


13  

You need other languages on the JVM for the same reason you need multiple programming languages in general: Different languages are better as solving different problems ... static typing vs. dynamic typing, strict vs. lazy ... Declarative, Imperative, Object Oriented ... etc.

您需要JVM上的其他语言,原因与您需要多种编程语言相同:不同的语言更好地解决了不同的问题...静态类型与动态类型,严格与懒惰...声明性,命令式,面向对象...等

In general, writing a "compiler" for another language to run on the JVM (or on the .Net CLR) is essentially a matter of compiling that language into java bytecode (or in the case of .Net, IL) instead of to assembly/machine language.

通常,为JVM(或.Net CLR)上运行的另一种语言编写“编译器”本质上是将该语言编译为java字节码(或者在.Net,IL的情况下)而不是汇编的问题。 /机器语言。

That said, a lot of the extra languages that are being written for JVM aren't compiled, but rather interpreted scripting languages...

也就是说,为JVM编写的许多额外语言都不是编译的,而是解释脚本语言......

#3


6  

Turning this on its head, consider you want to design a new language and you want it to run in a managed runtime with a JIT and GC. Then consider that you could:

转而考虑这一点,考虑您要设计一种新语言,并希望它在带有JIT和GC的托管运行时中运行。然后考虑你可以:

(a) write you own managed runtime (VM) and tackle all sorts of technically difficult issues that will doubtless lead to many bugs, bad performance, improper threading and a great deal of portability effort

(a)编写自己的托管运行时(VM)并处理各种技术难题,这些问题无疑会导致许多错误,性能不佳,线程不正确以及大量的可移植性工作

or

(b) compile your language into bytecode that can run on the Java VM which is already quite mature, fast and supported on a number of platforms (sometimes with more than one choice of vendor impementation).

(b)将您的语言编译为可以在Java VM上运行的字节码,该字符码已经非常成熟,快速并且在许多平台上得到支持(有时候有多个供应商强制选择)。

Given that the JavaVM bytecode is not tied so closely to the Java language as to unduly restrict the type of language you can implement, it has been a popular target environment for languages that want to run in a VM.

鉴于JavaVM字节码与Java语言没有太密切联系,以至于过度限制了您可以实现的语言类型,因此它一直是想要在VM中运行的语言的流行目标环境。

#4


4  

Java is a fairly verbose programming language that is getting outdated very quickly with all of the new fancy languages/frameworks coming out in the past 5 years. To support all the fancy syntax that people want in a language AND preserve backwards compatibility it makes more sense to add more languages to the runtime.

Java是一种相当冗长的编程语言,在过去的5年里,所有新的花哨语言/框架都会很快过时。为了支持人们在语言中想要的所有花哨语法并保持向后兼容性,更有意义的是向运行时添加更多语言。

Another benefit is it lets you run some web frameworks written in Ruby ala JRuby (aka Rails), or Grails(Groovy on Railys essentially), etc. on a proven hosting platform that likely already is in production at many companies, rather than having to using that not nearly as tried and tested Ruby hosting environments.

另一个好处是它允许你运行一些用Ruby ala JRuby(又名Rails)或Grails(基本上是Railys上的Groovy)等编写的Web框架,在一个经过验证的托管平台上运行,而这个托管平台很可能已经在许多公司生产,而不是必须使用它不像经过试验和测试的Ruby托管环境。

To compile the other languages you are just converting to Java byte code.

要编译其他语言,您只需转换为Java字节代码。

#5


2  

I would answer, “because Java sucks” but then again, perhaps that's too obvious … ;-)

我会回答,“因为Java糟透了”但又一次,也许这太明显了...... ;-)

#6


2  

The advantage to having other languages for the JVM is quite the same as the advantage to having other languages for computer in general: while all turing-complete languages can technically accomplish the same tasks, some languages make some tasks easier than others while other languages make other tasks easier. Since the JVM is something we already have the ability to run on all (well, nearly all) computers, and a lot of computers, in fact already have it, we can get the "write once, run anywhere" benefit, but without requiring that one uses Java.

为JVM提供其他语言的优势与为计算机提供其他语言的优势完全相同:虽然所有图灵完备语言在技术上可以完成相同的任务,但某些语言使某些任务比其他语言更容易。其他任务更容易。由于JVM已经能够在所有(好的,几乎所有)计算机上运行,​​而且很多计算机实际上已经拥有它,我们可以获得“一次编写,随处运行”的好处,但不需要那个使用Java。

Writing a language/compiler for the JVM isn't really different from writing one for a real machine. The real difference is that you have to compile to the JVM's bytecode instead of to the machine's executable code, but that's really a minor difference in the grand scheme of things.

为JVM编写语言/编译器与为真实机器编写语言/编译器没有什么不同。真正的区别在于你必须编译到JVM的字节码而不是机器的可执行代码,但这在宏观方案中确实是一个微小的差别。

Writing code for a language other than Java in the JVM really isn't different from writing Java except, of course, that you'll be using a different language. You'll compile using the compiler that somebody writes for it (again, not much different from a C compiler, fundamentally, and pretty much not different at all from a Java compiler), and you'll end up being able to run it just like you would compiled Java code since once it's in bytecode, the JVM can't tell what language it came from.

在JVM中为Java以外的语言编写代码与编写Java没有什么不同,当然,除了您将使用不同的语言。您将使用有人为其编写的编译器进行编译(再次,与C编译器没有太大区别,从根本上说,并且几乎没有与Java编译器完全不同),并且您最终只能运行它就像你编译Java代码一样,一旦它在字节码中,JVM就无法分辨出它来自哪种语言。

#7


2  

Different languages are tailored to different tasks. While certain problem domains fit the Java language perfectly, some are much easier to express in alternative languages. Also, for a user accustomed to Ruby, Python, etc, the ability to generate Java bytecode and take advantage of the JDK classes and JIT compiler has obvious benefits.

不同的语言适合不同的任务。虽然某些问题域完全符合Java语言,但有些域更容易用其他语言表达。此外,对于习惯于Ruby,Python等的用户来说,生成Java字节码并利用JDK类和JIT编译器的能力具有明显的好处。

#8


2  

Answering just your second question:

回答你的第二个问题:

The JVM is just an abstract machine and execution model. So targetting it with a compiler is just the same as any other machine and execution model that a compiler might target, be it implemented in hardware (x86, CELL, etc) or software (parrot, .NET). The JVM is fairly simple, so its actually a fairly easy target for compilers. Also, implementations tend to have pretty good JIT compilers (to deal with the lousy code that javac produces), so you can get good performance without having to worry about a lot of optimizations.

JVM只是一个抽象的机器和执行模型。因此,使用编译器对其进行定位与编译器可能针对的任何其他机器和执行模型相同,无论是在硬件(x86,CELL等)还是软件(parrot,.NET)中实现。 JVM非常简单,因此它实际上是编译器的一个相当容易的目标。此外,实现往往具有相当不错的JIT编译器(以处理javac产生的糟糕代码),因此您可以获得良好的性能而无需担心大量优化。

A couple of caveats apply. First, the JVM directly embodies java's module and inheritance system, so trying to do anything else (multiple inheritance, multiple dispatch) is likely to be tricky and require convoluted code. Second, JVMs are optimized to deal with the kind of bytecode that javac produces. Producing bytecode that is very different from this is likely to get into odd corners of the JIT compiler/JVM which will likely be inefficient at best (at worst, they can crash the JVM or at least give spurious VirtualMachineError exceptions).

有几点需要注意。首先,JVM直接体现了java的模块和继承系统,因此尝试执行任何其他操作(多重继承,多次调度)可能会非常棘手并且需要复杂的代码。其次,JVM经过优化,可以处理javac产生的字节码。生成与此非常不同的字节码可能会进入JIT编译器/ JVM的奇怪角落,这可能效率最低(在最坏的情况下,它们可能会使JVM崩溃或至少产生虚假的VirtualMachineError异常)。

#9


1  

What the JVM can do is defined by the JVM's bytecode (what you find in .class files) rather than the source language. So changing the high level source code language isn't going to have a substantial impact on the available functionality.

JVM可以做什么是由JVM的字节码(在.class文件中找到的)而不是源语言定义的。因此,更改高级源代码语言不会对可用功能产生实质性影响。

As for what is required to write a compiler for the JVM, all you really need to do is generate correct bytecode / .class files. How you write/compile code with an alternate compiler sort of depends on the compiler in question, but once the compiler outputs .class files, running them is no different than running the .class files generated by javac.

至于为JVM编写编译器需要什么,您真正需要做的就是生成正确的字节码/ .class文件。如何使用备用编译器编写/编译代码取决于所讨论的编译器,但是一旦编译器输出.class文件,运行它们与运行javac生成的.class文件没有什么不同。

#10


1  

The advantage for these other languages is that they get relatively easy access to lots of java libraries.

这些其他语言的优势在于它们可以相对容易地访问大量的Java库。

The advantage for Java people varies depending on language -- each has a story tell Java coders about what they do better. Some will stress how they can be used to add dynamic scripting to JVM-based apps, others will just talk about how their language is easier to use, has a better syntax, or so forth.

Java人员的优势因语言而异 - 每个人都有一个故事告诉Java编码员他们做得更好。有些人会强调如何使用它们为基于JVM的应用程序添加动态脚本,其他人只会谈论他们的语言如何更易于使用,具有更好的语法等等。

What's required are the same things to write any other language compiler: parsing to an AST, then transforming that to instructions for the target architecture (byte code) and storing it in the right format (.class files).

编写任何其他语言编译器所需要的是相同的东西:解析为AST,然后将其转换为目标体系结构的指令(字节代码)并以正确的格式(.class文件)存储它。

From the users' perspective, you just write code and run the compiler binaries, and out comes .class files you can mix in with those your java compiler produces.

从用户的角度来看,您只需编写代码并运行编译器二进制文件,然后输出.class文件,您可以将它们与您的Java编译器生成的文件混合使用。

#11


1  

The .NET languages are more for show than actual usefulness. Each language has been so butchered, that they're all C# with a new face.

.NET语言更多用于展示而非实际用途。每种语言都经过如此*,以至于他们都是新面孔的C#。

There are a variety of reasons to provide alternative languages for the Java VM:

为Java VM提供替代语言有多种原因:

  • The JVM is multiplatform. Any language ported to the JVM gets that as a free bonus.
  • JVM是多平台的。移植到JVM的任何语言都可以获得免费奖励。

  • There is quite a bit of legacy code out there. Antiquated engines like ColdFusion perform better while offering customers the ability to slowly phase their applications from the legacy solution to the modern solution.
  • 那里有相当多的遗留代码。像ColdFusion这样的过时引擎表现更好,同时为客户提供从传统解决方案到现代解决方案的缓慢阶段应用的能力。

  • Certain forms of scripting are better suited to rapid development. JavaFX, for example, is designed with rapid Graphical development in mind. In this way it competes with engines like DarkBasic. (Processing is another player in this space.)
  • 某些形式的脚本更适合快速开发。例如,JavaFX的设计考虑了快速的图形开发。通过这种方式,它可以与DarkBasic等引擎竞争。 (处理是这个领域的另一个参与者。)

  • Scripting environments can offer control. For example, an application may wish to expose a VBA-like environment to the user without exposing the underlying Java APIs. Using an engine like Rhino can provide an environment that supports quick and dirty coding in a carefully controlled sandbox.
  • 脚本环境可以提供控制。例如,应用程序可能希望向用户公开类似VBA的环境,而不暴露底层Java API。使用像Rhino这样的引擎可以在精心控制的沙箱中提供支持快速和脏编码的环境。

  • Interpreted scripts mean that there's no need to recompile anything. No need to recompile translates into a more dynamic environment. e.g. Despite OpenOffice's use of Java as a "scripting language", Java sucks for that use. The user has to go through all kinds of recompile/reload gyrations that are unnecessary in a dynamic scripting environment like Javascript.
  • 解释的脚本意味着不需要重新编译任何东西。无需重新编译即可转换为更加动态的环境。例如尽管OpenOffice使用Java作为“脚本语言”,但Java很糟糕。用户必须经历在动态脚本环境(如Javascript)中不必要的各种重新编译/重新加载回转。

  • Which brings me to another point. Scripting engines can be more easily stopped and reloaded without stopping and reloading the entire JVM. This increases the utility of the scripting language as the environment can be reset at any time.
  • 这让我想到另一点。在不停止和重新加载整个JVM的情况下,可以更轻松地停止和重新加载脚本引擎。这增加了脚本语言的实用性,因为可以随时重置环境。

#12


1  

It's much easier for a compiler writer to generate JVM or CLR byte-codes. They are a much cleaner and higher level abstraction than any machine language. Because of this, it is much more feasible to experiment with creating new languages than ever before, because all you have to do is target one of these VM architectures and you will have a set of tools and libraries already available for your language. They let language designers focus more on the language than all the necessary support infrastructure.

编译器编写器生成JVM或CLR字节代码要容易得多。它们比任何机器语言都更清晰,更高级别的抽象。因此,尝试创建新语言比以往任何时候都更加可行,因为您所要做的就是针对这些VM架构中的一个,并且您将拥有一套已经可用于您的语言的工具和库。他们让语言设计师更专注于语言,而不是所有必要的支持基础设施。

#13


1  

Because the JSR process is rendering Java more and more dead: http://www.infoq.com/news/2009/01/java7-updated

因为JSR进程使Java变得越来越死:http://www.infoq.com/news/2009/01/java7-updated

It's a shame that even essential and long known additions like Closures are not added just because the members cannot agree on an implementation.

令人遗憾的是,即使因为成员无法就实施达成一致,也不会添加像Closures这样的基本和长期已知的附加内容。

#14


1  

Java has accumulated a massive user base over seven major versions (from 1.0 to 1.6). Its capability to evolve is limited by the need to preserve backwards compatibility for the uncountable millions of lines of Java code running in production.

Java已经在七个主要版本(从1.0到1.6)上积累了庞大的用户群。它的发展能力受限于为生产中运行的无数数百万行Java代码保留向后兼容性的需要。

This is a problem because Java needs to evolve to:

这是一个问题,因为Java需要发展到:

  • compete with newer programming languages that have learned from Java's successes and failures.
  • 与从Java的成功和失败中学到的新编程语言竞争。

  • incorporate new advances in programming language design.
  • 融入编程语言设计的新进展。

  • allow users to take full advantage of advances in hardware - e.g. multi-core processors.
  • 允许用户充分利用硬件的进步 - 例如多核处理器。

  • fix some cutting edge ideas that introduced unexpected problems (e.g. checked exceptions, generics).
  • 修复引入意外问题的一些前沿想法(例如,检查异常,泛型)。

The requirement for backwards compatibility is a barrier to staying competitive.

向后兼容性的要求是保持竞争力的障碍。

If you compare Java to C#, Java has the advantage in mature, production ready libraries and frameworks, and a disadvantage in terms of language features and rate of increase in market share. This is what you would expect from comparing two successful languages that are one generation apart.

如果将Java与C#进行比较,Java在成熟的,生产就绪的库和框架中具有优势,并且在语言特性和市场份额增加率方面具有劣势。这是您对比较两代成功语言的期望。

Any new language has the same advantage and disadvantage that C# has compared to Java to an extreme degree. One way of maximizing the advantage in terms of language features, and minimizing the disadvantage in terms of mature libraries and frameworks is to build the language for an existing virtual machine and make it interoperable with code written for that virtual machine. This is the reason behind the modest success of Groovy and Clojure; and the excitement around Scala. Without the JVM these languages could only ever have occupied a tiny niche in a very specialized market segment, whereas with the JVM they occupy a significant niche in the mainstream.

任何新语言都具有与C#在极端程度上与Java相比的优点和缺点。在语言功能方面最大化优势并最小化成熟库和框架方面的缺点的一种方法是为现有虚拟机构建语言,并使其与为该虚拟机编写的代码互操作。这就是Groovy和Clojure取得适度成功背后的原因;和斯卡拉周围的兴奋。如果没有JVM,这些语言在一个非常专业的细分市场中只能占据一席之地,而在JVM中,它们在主流中占据了重要的位置。

#15


0  

They do it to keep up with .Net. .Net allows C#, VB, J# (formerly), F#, Python, Ruby (coming soon), and c++. I'm probably missing some. Probably the big one in there is Python, for the scripting people.

他们这样做是为了跟上.Net。 .Net允许C#,VB,J#(以前),F#,Python,Ruby(即将推出)和c ++。我可能错过了一些。对于脚本人来说,可能是那里最重要的是Python。

#16


0  

To an extent it is probably an 'Arms Race' against the .NET CLR.

在某种程度上,它可能是针对.NET CLR的“军备竞赛”。

But I think there are also genuine reasons for introducing new languages to the JVM, particularly when they will be run 'in parallel', you can use the right language for the right job, a scripting language like Groovy may be exactly what you need for your page presentation, whereas regular old Java is better for your business logic.

但是我认为将新语言引入JVM也是有​​正当理由的,特别是当它们“并行”运行时,你可以使用正确的语言来完成正确的工作,像Groovy这样的脚本语言可能正是你所需要的。您的页面显示,而常规的旧Java更适合您的业务逻辑。

I'm going to leave someone more qualified to talk about what is required to write a new language/compiler.

我将留下一个更有资格谈论编写新语言/编译器所需内容的人。

As for how to writing code, you do it in notepad/vi as usual! (or use a development tool that supports the language if you want to do it the easy way.) Compiling will require a special compiler for the language that will interpret and compile it into bytecode.

至于如何编写代码,你可以像往常一样在notepad / vi中进行编写! (或者如果你想以简单的方式进行操作,则使用支持该语言的开发工具。)编译将需要一个特殊的编译器来解释并将其编译成字节码。

Since java also produces bytecode technically you don't need to do anything special to run it.

由于java在技术上也会生成字节码,因此您无需执行任何特殊操作即可。

#17


0  

The reason is that the JVM platform offers a lot of advantages.

原因是JVM平台提供了很多优势。

  • Giant number of libraries
  • 庞大的图书馆数量

  • Broader degree of platform implementations
  • 更广泛的平台实施程度

  • Mature frameworks
  • Legacy code that's already part of your infrastructure
  • 已成为基础架构一部分的旧代码

The languages Sun is trying to support with their Scripting spec (e.g. Python, Ruby) are up and comers largely due to their perceived productivity enhancements. Running Jython allows you to, in theory, be more productive, and leverage the capabilities of Python to solve a problem more suited to Python, but still be able to integrate, on a runtime level, with your existing codebase. The classic implementations of Python and Ruby effect the same ability for C libraries.

Sun试图通过他们的脚本规范(例如Python,Ruby)来支持的语言很多,而且主要是因为他们认为生产力提高了。从理论上讲,运行Jython可以提高效率,并利用Python的功能来解决更适合Python的问题,但仍然能够在运行时级别与现有代码库集成。 Python和Ruby的经典实现对C库具有相同的能力。

Additionally, it's often easier to express some things in a dynamic language than in Java. If this is the case, you can go the other way; consume Python/Ruby libraries from Java.

此外,使用动态语言表达某些内容比使用Java更容易。如果是这种情况,你可以走另一条路;使用Java中的Python / Ruby库。

There's a performance hit, but many are willing to accept that in exchange for a less verbose, clearer codebase.

有一个性能受到打击,但许多人愿意接受这一点,以换取更简洁,更清晰的代码库。

#1


30  

To address your three questions separately:

分别解决您的三个问题:

What is the advantage in having other languages for the JVM?

使用JVM的其他语言有什么好处?

There are two factors here. (1) Why have a language other than Java for the JVM, and (2) why have another language run on the JVM, instead of a different runtime?

这里有两个因素。 (1)为什么JVM使用Java以外的语言,以及(2)为什么在JVM上运行另一种语言而不是运行不同的语言?

  1. Other languages can satisfy other needs. For example, Java has no built-in support for closures, a feature that is often very useful.
  2. 其他语言可以满足其他需求。例如,Java没有对闭包的内置支持,这一功能通常非常有用。

  3. A language that runs on the JVM is bytecode compatible with any other language that runs on the JVM, meaning that code written in one language can interact with a library written in another language.
  4. 在JVM上运行的语言与在JVM上运行的任何其他语言都是字节码兼容的,这意味着用一种语言编写的代码可以与用另一种语言编写的库交互。

What is required (in high level terms) to write a language/compiler for the JVM?

为JVM编写语言/编译器需要什么(在高级术语中)?

The JVM reads bytecode (.class) files to obtain the instructions it needs to perform. Thus any language that is to be run on the JVM needs to be compiled to bytecode adhering to the Sun specification. This process is similar to compiling to native code, except that instead of compiling to instructions understood by the CPU, the code is compiled to instructions that are interpreted by the JVM.

JVM读取字节码(.class)文件以获取它需要执行的指令。因此,任何要在JVM上运行的语言都需要编译为符合Sun规范的字节码。此过程类似于编译为本机代码,不同之处在于代码编译为由JVM解释的指令,而不是编译为CPU理解的指令。

How do you write/compile/run code in a language (other than Java) in the JVM?

如何用JVM中的一种语言(Java除外)编写/编译/运行代码?

Very much in the same way you write/compile/run code in Java. To get your feet wet, I'd recommend looking at Scala, which runs flawlessly on the JVM.

与在Java中编写/编译/运行代码的方式非常相似。为了让你的脚湿透,我建议你看一下在JVM上运行完美的Scala。

Answering your follow up questions:

回答你的后续问题:

How would an app written in, say, JPython, interact with a Java app?

如果用JPython编写的应用程序如何与Java应用程序交互?

This depends on the implementation's choice of bridging the language gap. In your example, Jython project has a straightforward means of doing this (see here):

这取决于实现选择弥合语言差距。在您的示例中,Jython项目有一个简单的方法来执行此操作(请参阅此处):

from java.net import URL
u = URL('http://jython.org')

Also, can that JPython application use any of the JDK functions/objects?

此外,JPython应用程序可以使用任何JDK函数/对象吗?

Yes, see above.

是的,见上文。

What if it was Jaskell code, would the fact that it is a functional language not make it incompatible with the JDK?

如果它是Jaskell代码,那么它是一种函数式语言会不会使它与JDK不兼容?

No. Scala (link above) for example implements functional features while maintaining compatibility with Java. For example:

例如,Scala(上面的链接)实现了功能特性,同时保持了与Java的兼容性。例如:

object Timer {
  def oncePerSecond(callback: () => unit) {
    while (true) { callback(); Thread sleep 1000 }
  }
  def timeFlies() {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies)
  }
}

#2


13  

You need other languages on the JVM for the same reason you need multiple programming languages in general: Different languages are better as solving different problems ... static typing vs. dynamic typing, strict vs. lazy ... Declarative, Imperative, Object Oriented ... etc.

您需要JVM上的其他语言,原因与您需要多种编程语言相同:不同的语言更好地解决了不同的问题...静态类型与动态类型,严格与懒惰...声明性,命令式,面向对象...等

In general, writing a "compiler" for another language to run on the JVM (or on the .Net CLR) is essentially a matter of compiling that language into java bytecode (or in the case of .Net, IL) instead of to assembly/machine language.

通常,为JVM(或.Net CLR)上运行的另一种语言编写“编译器”本质上是将该语言编译为java字节码(或者在.Net,IL的情况下)而不是汇编的问题。 /机器语言。

That said, a lot of the extra languages that are being written for JVM aren't compiled, but rather interpreted scripting languages...

也就是说,为JVM编写的许多额外语言都不是编译的,而是解释脚本语言......

#3


6  

Turning this on its head, consider you want to design a new language and you want it to run in a managed runtime with a JIT and GC. Then consider that you could:

转而考虑这一点,考虑您要设计一种新语言,并希望它在带有JIT和GC的托管运行时中运行。然后考虑你可以:

(a) write you own managed runtime (VM) and tackle all sorts of technically difficult issues that will doubtless lead to many bugs, bad performance, improper threading and a great deal of portability effort

(a)编写自己的托管运行时(VM)并处理各种技术难题,这些问题无疑会导致许多错误,性能不佳,线程不正确以及大量的可移植性工作

or

(b) compile your language into bytecode that can run on the Java VM which is already quite mature, fast and supported on a number of platforms (sometimes with more than one choice of vendor impementation).

(b)将您的语言编译为可以在Java VM上运行的字节码,该字符码已经非常成熟,快速并且在许多平台上得到支持(有时候有多个供应商强制选择)。

Given that the JavaVM bytecode is not tied so closely to the Java language as to unduly restrict the type of language you can implement, it has been a popular target environment for languages that want to run in a VM.

鉴于JavaVM字节码与Java语言没有太密切联系,以至于过度限制了您可以实现的语言类型,因此它一直是想要在VM中运行的语言的流行目标环境。

#4


4  

Java is a fairly verbose programming language that is getting outdated very quickly with all of the new fancy languages/frameworks coming out in the past 5 years. To support all the fancy syntax that people want in a language AND preserve backwards compatibility it makes more sense to add more languages to the runtime.

Java是一种相当冗长的编程语言,在过去的5年里,所有新的花哨语言/框架都会很快过时。为了支持人们在语言中想要的所有花哨语法并保持向后兼容性,更有意义的是向运行时添加更多语言。

Another benefit is it lets you run some web frameworks written in Ruby ala JRuby (aka Rails), or Grails(Groovy on Railys essentially), etc. on a proven hosting platform that likely already is in production at many companies, rather than having to using that not nearly as tried and tested Ruby hosting environments.

另一个好处是它允许你运行一些用Ruby ala JRuby(又名Rails)或Grails(基本上是Railys上的Groovy)等编写的Web框架,在一个经过验证的托管平台上运行,而这个托管平台很可能已经在许多公司生产,而不是必须使用它不像经过试验和测试的Ruby托管环境。

To compile the other languages you are just converting to Java byte code.

要编译其他语言,您只需转换为Java字节代码。

#5


2  

I would answer, “because Java sucks” but then again, perhaps that's too obvious … ;-)

我会回答,“因为Java糟透了”但又一次,也许这太明显了...... ;-)

#6


2  

The advantage to having other languages for the JVM is quite the same as the advantage to having other languages for computer in general: while all turing-complete languages can technically accomplish the same tasks, some languages make some tasks easier than others while other languages make other tasks easier. Since the JVM is something we already have the ability to run on all (well, nearly all) computers, and a lot of computers, in fact already have it, we can get the "write once, run anywhere" benefit, but without requiring that one uses Java.

为JVM提供其他语言的优势与为计算机提供其他语言的优势完全相同:虽然所有图灵完备语言在技术上可以完成相同的任务,但某些语言使某些任务比其他语言更容易。其他任务更容易。由于JVM已经能够在所有(好的,几乎所有)计算机上运行,​​而且很多计算机实际上已经拥有它,我们可以获得“一次编写,随处运行”的好处,但不需要那个使用Java。

Writing a language/compiler for the JVM isn't really different from writing one for a real machine. The real difference is that you have to compile to the JVM's bytecode instead of to the machine's executable code, but that's really a minor difference in the grand scheme of things.

为JVM编写语言/编译器与为真实机器编写语言/编译器没有什么不同。真正的区别在于你必须编译到JVM的字节码而不是机器的可执行代码,但这在宏观方案中确实是一个微小的差别。

Writing code for a language other than Java in the JVM really isn't different from writing Java except, of course, that you'll be using a different language. You'll compile using the compiler that somebody writes for it (again, not much different from a C compiler, fundamentally, and pretty much not different at all from a Java compiler), and you'll end up being able to run it just like you would compiled Java code since once it's in bytecode, the JVM can't tell what language it came from.

在JVM中为Java以外的语言编写代码与编写Java没有什么不同,当然,除了您将使用不同的语言。您将使用有人为其编写的编译器进行编译(再次,与C编译器没有太大区别,从根本上说,并且几乎没有与Java编译器完全不同),并且您最终只能运行它就像你编译Java代码一样,一旦它在字节码中,JVM就无法分辨出它来自哪种语言。

#7


2  

Different languages are tailored to different tasks. While certain problem domains fit the Java language perfectly, some are much easier to express in alternative languages. Also, for a user accustomed to Ruby, Python, etc, the ability to generate Java bytecode and take advantage of the JDK classes and JIT compiler has obvious benefits.

不同的语言适合不同的任务。虽然某些问题域完全符合Java语言,但有些域更容易用其他语言表达。此外,对于习惯于Ruby,Python等的用户来说,生成Java字节码并利用JDK类和JIT编译器的能力具有明显的好处。

#8


2  

Answering just your second question:

回答你的第二个问题:

The JVM is just an abstract machine and execution model. So targetting it with a compiler is just the same as any other machine and execution model that a compiler might target, be it implemented in hardware (x86, CELL, etc) or software (parrot, .NET). The JVM is fairly simple, so its actually a fairly easy target for compilers. Also, implementations tend to have pretty good JIT compilers (to deal with the lousy code that javac produces), so you can get good performance without having to worry about a lot of optimizations.

JVM只是一个抽象的机器和执行模型。因此,使用编译器对其进行定位与编译器可能针对的任何其他机器和执行模型相同,无论是在硬件(x86,CELL等)还是软件(parrot,.NET)中实现。 JVM非常简单,因此它实际上是编译器的一个相当容易的目标。此外,实现往往具有相当不错的JIT编译器(以处理javac产生的糟糕代码),因此您可以获得良好的性能而无需担心大量优化。

A couple of caveats apply. First, the JVM directly embodies java's module and inheritance system, so trying to do anything else (multiple inheritance, multiple dispatch) is likely to be tricky and require convoluted code. Second, JVMs are optimized to deal with the kind of bytecode that javac produces. Producing bytecode that is very different from this is likely to get into odd corners of the JIT compiler/JVM which will likely be inefficient at best (at worst, they can crash the JVM or at least give spurious VirtualMachineError exceptions).

有几点需要注意。首先,JVM直接体现了java的模块和继承系统,因此尝试执行任何其他操作(多重继承,多次调度)可能会非常棘手并且需要复杂的代码。其次,JVM经过优化,可以处理javac产生的字节码。生成与此非常不同的字节码可能会进入JIT编译器/ JVM的奇怪角落,这可能效率最低(在最坏的情况下,它们可能会使JVM崩溃或至少产生虚假的VirtualMachineError异常)。

#9


1  

What the JVM can do is defined by the JVM's bytecode (what you find in .class files) rather than the source language. So changing the high level source code language isn't going to have a substantial impact on the available functionality.

JVM可以做什么是由JVM的字节码(在.class文件中找到的)而不是源语言定义的。因此,更改高级源代码语言不会对可用功能产生实质性影响。

As for what is required to write a compiler for the JVM, all you really need to do is generate correct bytecode / .class files. How you write/compile code with an alternate compiler sort of depends on the compiler in question, but once the compiler outputs .class files, running them is no different than running the .class files generated by javac.

至于为JVM编写编译器需要什么,您真正需要做的就是生成正确的字节码/ .class文件。如何使用备用编译器编写/编译代码取决于所讨论的编译器,但是一旦编译器输出.class文件,运行它们与运行javac生成的.class文件没有什么不同。

#10


1  

The advantage for these other languages is that they get relatively easy access to lots of java libraries.

这些其他语言的优势在于它们可以相对容易地访问大量的Java库。

The advantage for Java people varies depending on language -- each has a story tell Java coders about what they do better. Some will stress how they can be used to add dynamic scripting to JVM-based apps, others will just talk about how their language is easier to use, has a better syntax, or so forth.

Java人员的优势因语言而异 - 每个人都有一个故事告诉Java编码员他们做得更好。有些人会强调如何使用它们为基于JVM的应用程序添加动态脚本,其他人只会谈论他们的语言如何更易于使用,具有更好的语法等等。

What's required are the same things to write any other language compiler: parsing to an AST, then transforming that to instructions for the target architecture (byte code) and storing it in the right format (.class files).

编写任何其他语言编译器所需要的是相同的东西:解析为AST,然后将其转换为目标体系结构的指令(字节代码)并以正确的格式(.class文件)存储它。

From the users' perspective, you just write code and run the compiler binaries, and out comes .class files you can mix in with those your java compiler produces.

从用户的角度来看,您只需编写代码并运行编译器二进制文件,然后输出.class文件,您可以将它们与您的Java编译器生成的文件混合使用。

#11


1  

The .NET languages are more for show than actual usefulness. Each language has been so butchered, that they're all C# with a new face.

.NET语言更多用于展示而非实际用途。每种语言都经过如此*,以至于他们都是新面孔的C#。

There are a variety of reasons to provide alternative languages for the Java VM:

为Java VM提供替代语言有多种原因:

  • The JVM is multiplatform. Any language ported to the JVM gets that as a free bonus.
  • JVM是多平台的。移植到JVM的任何语言都可以获得免费奖励。

  • There is quite a bit of legacy code out there. Antiquated engines like ColdFusion perform better while offering customers the ability to slowly phase their applications from the legacy solution to the modern solution.
  • 那里有相当多的遗留代码。像ColdFusion这样的过时引擎表现更好,同时为客户提供从传统解决方案到现代解决方案的缓慢阶段应用的能力。

  • Certain forms of scripting are better suited to rapid development. JavaFX, for example, is designed with rapid Graphical development in mind. In this way it competes with engines like DarkBasic. (Processing is another player in this space.)
  • 某些形式的脚本更适合快速开发。例如,JavaFX的设计考虑了快速的图形开发。通过这种方式,它可以与DarkBasic等引擎竞争。 (处理是这个领域的另一个参与者。)

  • Scripting environments can offer control. For example, an application may wish to expose a VBA-like environment to the user without exposing the underlying Java APIs. Using an engine like Rhino can provide an environment that supports quick and dirty coding in a carefully controlled sandbox.
  • 脚本环境可以提供控制。例如,应用程序可能希望向用户公开类似VBA的环境,而不暴露底层Java API。使用像Rhino这样的引擎可以在精心控制的沙箱中提供支持快速和脏编码的环境。

  • Interpreted scripts mean that there's no need to recompile anything. No need to recompile translates into a more dynamic environment. e.g. Despite OpenOffice's use of Java as a "scripting language", Java sucks for that use. The user has to go through all kinds of recompile/reload gyrations that are unnecessary in a dynamic scripting environment like Javascript.
  • 解释的脚本意味着不需要重新编译任何东西。无需重新编译即可转换为更加动态的环境。例如尽管OpenOffice使用Java作为“脚本语言”,但Java很糟糕。用户必须经历在动态脚本环境(如Javascript)中不必要的各种重新编译/重新加载回转。

  • Which brings me to another point. Scripting engines can be more easily stopped and reloaded without stopping and reloading the entire JVM. This increases the utility of the scripting language as the environment can be reset at any time.
  • 这让我想到另一点。在不停止和重新加载整个JVM的情况下,可以更轻松地停止和重新加载脚本引擎。这增加了脚本语言的实用性,因为可以随时重置环境。

#12


1  

It's much easier for a compiler writer to generate JVM or CLR byte-codes. They are a much cleaner and higher level abstraction than any machine language. Because of this, it is much more feasible to experiment with creating new languages than ever before, because all you have to do is target one of these VM architectures and you will have a set of tools and libraries already available for your language. They let language designers focus more on the language than all the necessary support infrastructure.

编译器编写器生成JVM或CLR字节代码要容易得多。它们比任何机器语言都更清晰,更高级别的抽象。因此,尝试创建新语言比以往任何时候都更加可行,因为您所要做的就是针对这些VM架构中的一个,并且您将拥有一套已经可用于您的语言的工具和库。他们让语言设计师更专注于语言,而不是所有必要的支持基础设施。

#13


1  

Because the JSR process is rendering Java more and more dead: http://www.infoq.com/news/2009/01/java7-updated

因为JSR进程使Java变得越来越死:http://www.infoq.com/news/2009/01/java7-updated

It's a shame that even essential and long known additions like Closures are not added just because the members cannot agree on an implementation.

令人遗憾的是,即使因为成员无法就实施达成一致,也不会添加像Closures这样的基本和长期已知的附加内容。

#14


1  

Java has accumulated a massive user base over seven major versions (from 1.0 to 1.6). Its capability to evolve is limited by the need to preserve backwards compatibility for the uncountable millions of lines of Java code running in production.

Java已经在七个主要版本(从1.0到1.6)上积累了庞大的用户群。它的发展能力受限于为生产中运行的无数数百万行Java代码保留向后兼容性的需要。

This is a problem because Java needs to evolve to:

这是一个问题,因为Java需要发展到:

  • compete with newer programming languages that have learned from Java's successes and failures.
  • 与从Java的成功和失败中学到的新编程语言竞争。

  • incorporate new advances in programming language design.
  • 融入编程语言设计的新进展。

  • allow users to take full advantage of advances in hardware - e.g. multi-core processors.
  • 允许用户充分利用硬件的进步 - 例如多核处理器。

  • fix some cutting edge ideas that introduced unexpected problems (e.g. checked exceptions, generics).
  • 修复引入意外问题的一些前沿想法(例如,检查异常,泛型)。

The requirement for backwards compatibility is a barrier to staying competitive.

向后兼容性的要求是保持竞争力的障碍。

If you compare Java to C#, Java has the advantage in mature, production ready libraries and frameworks, and a disadvantage in terms of language features and rate of increase in market share. This is what you would expect from comparing two successful languages that are one generation apart.

如果将Java与C#进行比较,Java在成熟的,生产就绪的库和框架中具有优势,并且在语言特性和市场份额增加率方面具有劣势。这是您对比较两代成功语言的期望。

Any new language has the same advantage and disadvantage that C# has compared to Java to an extreme degree. One way of maximizing the advantage in terms of language features, and minimizing the disadvantage in terms of mature libraries and frameworks is to build the language for an existing virtual machine and make it interoperable with code written for that virtual machine. This is the reason behind the modest success of Groovy and Clojure; and the excitement around Scala. Without the JVM these languages could only ever have occupied a tiny niche in a very specialized market segment, whereas with the JVM they occupy a significant niche in the mainstream.

任何新语言都具有与C#在极端程度上与Java相比的优点和缺点。在语言功能方面最大化优势并最小化成熟库和框架方面的缺点的一种方法是为现有虚拟机构建语言,并使其与为该虚拟机编写的代码互操作。这就是Groovy和Clojure取得适度成功背后的原因;和斯卡拉周围的兴奋。如果没有JVM,这些语言在一个非常专业的细分市场中只能占据一席之地,而在JVM中,它们在主流中占据了重要的位置。

#15


0  

They do it to keep up with .Net. .Net allows C#, VB, J# (formerly), F#, Python, Ruby (coming soon), and c++. I'm probably missing some. Probably the big one in there is Python, for the scripting people.

他们这样做是为了跟上.Net。 .Net允许C#,VB,J#(以前),F#,Python,Ruby(即将推出)和c ++。我可能错过了一些。对于脚本人来说,可能是那里最重要的是Python。

#16


0  

To an extent it is probably an 'Arms Race' against the .NET CLR.

在某种程度上,它可能是针对.NET CLR的“军备竞赛”。

But I think there are also genuine reasons for introducing new languages to the JVM, particularly when they will be run 'in parallel', you can use the right language for the right job, a scripting language like Groovy may be exactly what you need for your page presentation, whereas regular old Java is better for your business logic.

但是我认为将新语言引入JVM也是有​​正当理由的,特别是当它们“并行”运行时,你可以使用正确的语言来完成正确的工作,像Groovy这样的脚本语言可能正是你所需要的。您的页面显示,而常规的旧Java更适合您的业务逻辑。

I'm going to leave someone more qualified to talk about what is required to write a new language/compiler.

我将留下一个更有资格谈论编写新语言/编译器所需内容的人。

As for how to writing code, you do it in notepad/vi as usual! (or use a development tool that supports the language if you want to do it the easy way.) Compiling will require a special compiler for the language that will interpret and compile it into bytecode.

至于如何编写代码,你可以像往常一样在notepad / vi中进行编写! (或者如果你想以简单的方式进行操作,则使用支持该语言的开发工具。)编译将需要一个特殊的编译器来解释并将其编译成字节码。

Since java also produces bytecode technically you don't need to do anything special to run it.

由于java在技术上也会生成字节码,因此您无需执行任何特殊操作即可。

#17


0  

The reason is that the JVM platform offers a lot of advantages.

原因是JVM平台提供了很多优势。

  • Giant number of libraries
  • 庞大的图书馆数量

  • Broader degree of platform implementations
  • 更广泛的平台实施程度

  • Mature frameworks
  • Legacy code that's already part of your infrastructure
  • 已成为基础架构一部分的旧代码

The languages Sun is trying to support with their Scripting spec (e.g. Python, Ruby) are up and comers largely due to their perceived productivity enhancements. Running Jython allows you to, in theory, be more productive, and leverage the capabilities of Python to solve a problem more suited to Python, but still be able to integrate, on a runtime level, with your existing codebase. The classic implementations of Python and Ruby effect the same ability for C libraries.

Sun试图通过他们的脚本规范(例如Python,Ruby)来支持的语言很多,而且主要是因为他们认为生产力提高了。从理论上讲,运行Jython可以提高效率,并利用Python的功能来解决更适合Python的问题,但仍然能够在运行时级别与现有代码库集成。 Python和Ruby的经典实现对C库具有相同的能力。

Additionally, it's often easier to express some things in a dynamic language than in Java. If this is the case, you can go the other way; consume Python/Ruby libraries from Java.

此外,使用动态语言表达某些内容比使用Java更容易。如果是这种情况,你可以走另一条路;使用Java中的Python / Ruby库。

There's a performance hit, but many are willing to accept that in exchange for a less verbose, clearer codebase.

有一个性能受到打击,但许多人愿意接受这一点,以换取更简洁,更清晰的代码库。