You're trying to execute "C:/". You'll want to execute something like:

"javaw.exe d:\\somejavaprogram\\program.jar"

Notice the path separators.

I'm assuming this is for an ad-hoc project, rather than something large. However, for best practice running external programs from code:

  • Don't hardcode the executable location, unless you're certain it will never change
  • Look up directories like %windir% using System.getenv
  • Don't assume programs like javaw.exe are in the search path: check them first, or allow the user to specify a location
  • Make sure you're taking spaces into account: "cmd /c start " + myProg will not work if myProg is "my program.jar".
Answer from Mark on Stack Overflow
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 71874858 โ€บ how-do-i-execute-java-code-inside-a-java-program
How do I execute java code INSIDE a java program? - Stack Overflow
... You could save the contents of the text field to a file with the .java extension, then compile it and run the corresponding binary with Runtime.getRuntime().exec("javac " + [insert the name of your java file here]); and Runtime.getRunti...
Top answer
1 of 14
10

You're trying to execute "C:/". You'll want to execute something like:

"javaw.exe d:\\somejavaprogram\\program.jar"

Notice the path separators.

I'm assuming this is for an ad-hoc project, rather than something large. However, for best practice running external programs from code:

  • Don't hardcode the executable location, unless you're certain it will never change
  • Look up directories like %windir% using System.getenv
  • Don't assume programs like javaw.exe are in the search path: check them first, or allow the user to specify a location
  • Make sure you're taking spaces into account: "cmd /c start " + myProg will not work if myProg is "my program.jar".
2 of 14
8

You can either launch another JVM (as described in detail in other answers). But that is not a solution i would prefer.

Reasons are:

  • calling a native program from java is "dirty" (and sometimes crashes your own VM)
  • you need to know the path to the external JVM (modern JVMs don't set JAVA_HOME anymore)
  • you have no control on the other program

Main reason to do it anyway is, that the other application has no control over your part of the program either. And more importantly there's no trouble with unresponsive system threads like the AWT-Thread if the other application doesn't know its threading 101.

But! You can achieve more control and similar behaviour by using an elementary plugin technique. I.e. just call "a known interface method" the other application has to implement. (in this case the "main" method).

Only it's not quite as easy as it sounds to pull this off.

  • you have to dynamically include required jars at runtime (or include them in the classpath for your application)
  • you have to put the plugin in a sandbox that prevents compromising critical classes to the other application

And this calls for a customized classloader. But be warned - there are some well hidden pitfalls in implementing that. On the other hand it's a great exercise.

So, take your pick: either quick and dirty or hard but rewarding.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - Then we need to execute/run the class file. We need to use the command javac file_name_with_the_extension. For example, as I want to compile my Main.java, I will use the command javac Main.java.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - If you are writing code that needs to run on an older version of Java, you can use the --release flag: ... This makes sure that the compiled bytecode remains compatible with Java 8 environments, even if you are using a newer version of the compiler. Once a Java program has been compiled into a .class file, it can be executed using the java command.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ compilation-execution-java-program
Compilation and Execution of a Java Program - GeeksforGeeks
October 14, 2025 - Geeks.java is the file to compile. ... If there are no errors, this command will create a file called Geeks.class. The .class file contains bytecode, which is a platform-independent code that the JVM can execute. If there are errors, the compiler will show them in the terminal.
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ idea โ€บ run-java-applications.html
Tutorial: Run a Java application | IntelliJ IDEA Documentation
February 11, 2026 - In the New Project list on the left, select Java. Give the project a name (for example, RunApplication). Select Maven as the Build system and select the Add sample code checkbox.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-compile-and-run-java-program
How to Compile and Run Java Program - Javatpoint
How to Compile and Run Java Program - How to Compile and Run Java Program with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @Mark.io โ€บ running-arbitrary-java-code-on-the-fly-d5003ebd46a8
Running Arbitrary Java Code On The Fly | by Murat Kilic | Medium
September 13, 2019 - As for executor code itself, after we skip the code for parsing command line options, we end up at this piece of code, where jarName and className are arguments to the program. This is where we locate the jar file and load the class and create an instance of it (to be used by invoke method later) ClassLoader classLoader = JavaExecutor.class.getClassLoader(); Path jarPath = Paths.get(jarName); URLClassLoader urlClassLoader = new URLClassLoader( new URL[] {jarPath.toUri().toURL()}, classLoader); Class executableClass = urlClassLoader.loadClass(className); Object obj1=executableClass.newInstance();
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 11hello
1.1 Your First Java Program: Hello World
June 10, 2022 - Whenever this program is executed, it reads the command-line argument that you type after the program name and prints it back out to the terminal as part of the message. Write a program TenHelloWorlds.java that prints "Hello, World" ten times. Modify UseArgument.java to make a program UseThree.java that takes three names and prints out a proper sentence with the names in the reverse of the order given, so that for example, "java UseThree Alice Bob Carol" gives "Hi Carol, Bob, and Alice.".
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ i โ€บ 7.4.0
Calling another Java program with java.lang.Runtime.exec()
October 7, 2025 - import java.io.*; public class CallHelloPgm { public static void main(String args[]) { Process theProcess = null; BufferedReader inStream = null; System.out.println("CallHelloPgm.main() invoked"); // call the Hello class try { theProcess = Runtime.getRuntime().exec("java QIBMHello"); } catch(IOException e) { System.err.println("Error on exec() method"); e.printStackTrace(); } // read from the called program's standard output stream try { inStream = new BufferedReader( new InputStreamReader( theProcess.getInputStream() )); System.out.println(inStream.readLine()); } catch(IOException e) { System.err.println("Error on inStream.readLine()"); e.printStackTrace(); } } }
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_getstarted.asp
Java Getting Started
This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java Main" to run the file: ... Congratulations! You have written and executed your first Java program.
๐ŸŒ
Swarthmore College
cs.swarthmore.edu โ€บ ~newhall โ€บ unixhelp โ€บ debuggingtips_Java.html
Compiling, Running and Debugging Java Programs
Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code: % java &ltclass with main method to run&gt [&ltcommand line args&gt, ...] For example, to run the main method from the Foo class:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-run-a-java-program
What are the different steps involved to execute a Java program?
Interpreter: It reads and executes one bytecode instruction at a time. JIT Compiler: It stands for Just-In-Time Compiler. It converts bytecode into machine code at runtime.
๐ŸŒ
Dev.java
dev.java โ€บ learn โ€บ launch-simple-source-code-programs
Launching Simple Source-Code Programs - Dev.java
December 23, 2025 - So this example, referencing the Scanner and MatchResult classes, can be executed simply with the java launcher: ... However the below example referencing RandomUtils, part of the Apache Commons Lang, will need to have the commons-lang.jar added to the classpath at launch: ... On a Unix-like operating system, a single-file source-code application, can also be launched as a shebang file like a script. Within a java source file, as the first line in the file add path/to/java/home --source <version> like in the below example:
Top answer
1 of 3
2

tl;dr

In modern Java, as a convenience to beginner Java students, the java app will graciously compile, and then run, a .java file.

Think of it as java app detecting a source-code file, then sub-contracting the compilation work out to javac app, caching the compiled class in memory, and lastly continuing onwards to run the newly compiled class.

Details

A JDK comes bundled with a few dozen tools. A couple of these are crucial:

  • javac โ€” Compiles .java file of source code text into .class file of bytecode.
  • java โ€” Execute that .class file by launching a JVM.

The process steps are:

  1. You write Java source code in a .java file, including a main method inside a class definition.
  2. You submit that file to a compiler, such as the javac app bundled with every JDK.
  3. The compiler outputs a .class file.
  4. You execute (run) the main method by launching the java app bundled with every JDK.

You can skip some steps.

  • Compile and run a single file:
    As of Java 11, you can point the java app to a .java file. The java app will automatically compile first, as if javac were called on your behalf. Then the java app will go on to run the newly compiled class.
    • No .class file is written to storage. The compiled class is cached in memory.
    • See JEP 330: Launch Single-File Source-Code Programs for all the details.
    • This behavior is what you noted: However, java HelloWorld.java was executed properly without making HelloWorld.class.
  • Simplified main
    As a preview feature in Java 21, you can:
    • Write your java file without declaring a class. Just write the main method alone, with nothing around it.
    • Drop the public static from your main method declaration.
    • See JEP 445: Unnamed Classes and Instance Main Methods (Preview) for details.

Motivation

The Java team is making concerted efforts to ease the way for beginning Java programmers, to smooth over the initial speed bumps. They are trying to mask some of the elaborate ceremony, and make the tooling more friendly and accommodating.

The underlying structures and features of Java are still in place. This is not a โ€œdumbing-downโ€ of Java. These efforts are just attempts at making reasonable accommodations for beginners.

Hello World

Combining these features, getting started with a Hello World app is a simple as writing the following in a HelloWorld.java file:

void main() {
    System.out.println("Hello, World!");
}

โ€ฆ and then on the command-line calling:

java HelloWorld.java

Result on the console:

Hello, World!

That is a vast reduction in labor and confusion for a brand-new student of Java.


jshell

By the wayโ€ฆ Another piece of making Java easier to use is jshell, a REPL for Java.

Both beginners and pros find jshell handy for running short bits of Java with immediate feedback.

BlueJ

Another useful tool for beginners is BlueJ, an IDE designed for students.

2 of 3
1

As mentioned in the JEP-330, for your example

java HelloWorld.java

is informally equivalent to

javac -d <memory> HelloWorld.java
java -cp <memory> hello.World
๐ŸŒ
Unibz
inf.unibz.it โ€บ ~calvanese โ€บ teaching โ€บ 04-05-ip โ€บ lecture-notes โ€บ uni01 โ€บ node15.html
Write, compile, and execute a Java program
The standard Java compiler, which is part of the Java Standard Development Kit (Java SDK), is javac. To use it, you have to execute the command: ... The compilation produces as a result a file called ClassName.class, which contains the command that can be directly executed by the computer. For example: ... A program can be executed only after it has been compiled, i.e., when we have the file ClassName.class. In Java the execution of a program is done through the command