Cris
Cris

Reputation: 128

Java Compiler multi-platform

How could I compile a Main.Java program to something that I could run with OpenJDK Java 6 or 7 . The program doesn't contain anything besides displaying the message "Hello World" I would like to compiler to .JAR

Also if I compile the program will it run in Windows or do I got to cross compile like C++ if I'm using Linux as the native developer.

I'm using the Linux Ubuntu OpenJDK Development Kit (JDK)

This is how you compile.

cristian@ubuntu:~/Java$ javac Main.java

but how would you compile to a .JAR file so I can run it in my Windows. Any guides to learning Java are welcome and thanks for the help. I'm using Linux Ubuntu.

Upvotes: 0

Views: 421

Answers (2)

Jason Braucht
Jason Braucht

Reputation: 2368

You must specify your main class in the JARs manifest in order for it to be runnable. You can then invoke your application from it's jar as follows

java -jar application.jar

Current versions of the JDK for Windows will create a file association for .jar files so if you click on them they will execute.

For further details, take a look at Oracle's Jar Tutorial, specifically the section on Setting an Application's Entry Point

Edit

Regarding the need for cross compilation... Since Java compiler targets a virtual machine, you do not need to cross compile for different operating systems. (One of the early taglines for Java was "Write Once Run Anywhere".)

Here is a decent article on the basics of how the JVM does its magic: What Is The Java Virtual Machine & How Does It Work?

The Oracle Java Tutorials are an solid starting point for many things java related and I recommend reading through them if you are new to Java.

Upvotes: 3

tom
tom

Reputation: 354

Java's JAR files are platform independent and only require an installed JRE/JDK. OpenJDK does almost the same thing as Oracle JDK however you can also get the Oracle JDK on Linux Ubuntu by using third-party repositories such as ppa:webupd8team/java(details on how to use the repository are provided on the webupd8 website) i found i needed to switch to oracle java for performance issues.

Most common IDE's for java provide support for compiling and packaging to JAR/Runnable JAR.

A popular java IDE is Eclipse(http://eclipse.org) other popular IDE's include NetBeans and JetBrains IntelliJ IDEA.

Most programmers use IDE's for ease of use and other functionality such as IntelliSense(Autocomplete), error checking, Syntax highlighting and debugging.

A good guide to learning java is http://www.youtube.com/playlist?list=PLFE2CE09D83EE3E28. Other guides such as java game development can also be found on Thenewboston's channel or thenewboston website.

Upvotes: 1

Related Questions