Im in my first week of college majoring in computer science. Can java source code be directly executed by JVM?

  • 1
    read up on bytecode. It explains what the JVM does. javac takes the java source code to bytecode. – Erik Eidt Sep 1 '16 at 22:35
  • By takes it to he means compiles it to. If you're going to think a programmers every time you're stuck on a question can I suggest you search a little harder for an answer first. We do design questions here. – candied_orange Sep 1 '16 at 22:37

No, it can't.

The name "Java VM" is a bit of a misnomer. The JVM came about as a part of the Java platform, but it is actually independent from Java. Neither does the JVM execute Java (it executes its own language, which AFAIK doesn't have a name, but is usually called JVM bytecode, Java bytecode (another misnomer), or sometimes JVML) nor does Java require a JVM (you could just as well interpret Java directly, compile it to native machine code, compile it to ECMAScript, compile it to CLI CIL bytecode, etc.)

The Java platform consists of three components:

  • The Java Programming Language
  • The JVM
  • The Libraries

They are all independent from one another. For example, you can use Java without the JVM and the Libraries, e.g. on Android. Android has a different bytecode format for a different VM, and it has different libraries. You can also use the JVM and Libraries without Java, e.g. from Scala, Clojure, ECMAScript, Ruby, Python, PHP, Fantom, Groovy, Frege, and so on.

| improve this answer | |

No. Java VM cannot run (interpret) source code. Code must be compiled first, generating a byte-code file (.class) that can be interpreted and run by the JVMs. Java compilation doesn't generate native executables either.

Bytecode is an intermediate instruction set that is platform independant and so can be run in different OS's JVMs. Each JVM is compiled for its host OS which allows it to interpret the bytecode and compile it into native CPU instructions during runtime.

| improve this answer | |

Short answer - no.

By way of an analogy:

Can you run your car on dinosaurs?

Of course not. Even if there were some around to try it with, they wouldn't fit (or want to get) in the tank.

Can you run your car on the bacteria, heat and pressured-treated remains of dinosaurs?

Yes. We call that petroleum.

You and I write source code.
A compiler converts that into byte code that we store in [jar] files. The Java RunTime can link-load those files into something that a computer can actually execute. There's no short cut.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.