Java Everywhere: How to Write Code Once and Run on Windows, Linux, and macOS

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Java is the King of Cross-Platform?



Have you ever wondered why banking systems, Android apps, and even games like Minecraft work on completely different devices? The secret is simple — cross-platform development in Java. This technology allows you to write code once and run it anywhere: on Windows, Linux, macOS, or even on a smart refrigerator.



In this article, we won't just talk about theory. We'll write real code, delve into the magic of the Java Virtual Machine (JVM), and learn how to avoid common pitfalls. If you're tired of the phrase "it works on my machine" — welcome to the world of Java!



How Does Java Trick the Operating System?



The main secret of cross-platform capability is not magic, but an intermediary layer called the JVM (Java Virtual Machine). When you write code in Java, you compile it not into machine code, but into bytecode (.class files). This bytecode is universal, like Esperanto for computers. The JVM then translates it into the language of the specific OS.



Take a look at this simplest code that works everywhere:



public class HelloWorld {    public static void main(String[] args) {        System.out.println("Hello, world! I am running on " + System.getProperty("os.name"));    }}


Run this code on Windows — you'll see "Windows 10". On Linux — "Linux". On macOS — "Mac OS X". Without a single change in the code. This is cross-platform development in Java in action.



How Does the JIT Compiler Work?



When you run a Java program, the JVM doesn't just interpret bytecode line by line. It uses a JIT (Just-In-Time) compiler. It analyzes which parts of the code are executed most frequently and compiles them into machine code "on the fly." Therefore, Java applications get faster over time, not slower.



Where Java Stumbles: Pitfalls of Cross-Platform Development



Despite all the magic of the JVM, there are three main stumbling blocks that even experienced developers trip over:



  • File paths — Windows uses a backslash (\\), Linux uses a forward slash (/).
  • Encodings — Windows defaults to Windows-1251, Linux defaults to UTF-8.
  • Graphics and native code — Swing works everywhere, but looks different.


Let's break down each problem with code examples.



Problem #1: File Paths



Never write paths manually! Use File.separator or Paths.get():



import java.nio.file.*;

public class PathExample { public static void main(String[] args) { // Correct: cross-platform Path path = Paths.get("data", "config", "settings.xml"); System.out.println("Path: " + path.toString()); // Incorrect: will break on Linux // String badPath = "data\\\\config\\\\settings.xml"; }}


On Windows you will see data\\config\\settings.xml, on Linux — data/config/settings.xml. Java will automatically insert the correct separator.



Problem #2: Encodings



Reading text files is a classic trap. Always specify the encoding explicitly:



import java.io.*;import java.nio.charset.StandardCharsets;

public class EncodingExample { public static void main(String[] args) throws IOException { // Correct: UTF-8 everywhere BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream("file.txt"), StandardCharsets.UTF_8 ) ); String line = reader.readLine(); System.out.println(line); reader.close(); }}


If you don't specify the encoding, on Windows you risk getting garbled text instead of Russian text.



Writ

Blogs

Book Recommendations