🌐
OneCompiler
onecompiler.com β€Ί java
Java Online Compiler
OneCompiler's Online Java Editor helps you write, compile, run and debug Java code online. The code runs on latest JDK & JRE
🌐
JDoodle
jdoodle.com β€Ί online-java-compiler
Online Java Compiler - Run Java Code in Browser
JDoodle is an Online Compiler, Editor, IDE for Java, C, C++, PHP, Perl, Python, Ruby and many more. You can run your programs on the fly online, and you can save and share them with others. Quick and Easy way to compile and run programs online.
🌐
Programiz
programiz.com β€Ί java-programming β€Ί online-compiler
Online Java Compiler - Programiz
// Online Java Compiler // Use this editor to write, compile and run your Java code online class Main { public static void main(String[] args) { System.out.println("Try programiz.pro"); } } Output Β·
🌐
OnlineGDB
onlinegdb.com β€Ί online_java_compiler
Online Java Compiler - online editor
Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } close ad [x] Done Β·
🌐
Compilejava
compilejava.net
Online Java IDE
Simple, fast and secure Online Java IDE / Compiler
🌐
W3Schools
w3schools.com β€Ί java β€Ί java_compiler.asp
Java Online Compiler (Editor / Interpreter)
With our online Java compiler, you can edit Java code, and view the result in your browser. public class Main { public static void main(String[] args) { System.out.println("Hello World!"); } } Hello World!
🌐
CodeUtility
java.codeutility.io
Run Java Code Online - Online Java Compiler
HTML+ERB to Haml HTML+ERB to Slim ... Minify CSS Minify JSON Minify SQL ... Compile and run Java classes online using a CLI-style toolβ€”no JDK installation required....
🌐
Online Java
online-java.com
Online Java - IDE, Code Editor, Compiler
Write, compile, and run Java code online with our fast and reliable online compiler and IDE.
Find elsewhere
🌐
Replit
webflow.replit.com β€Ί language β€Ί online-java-compiler
Java Online Compiler & Interpreter | Replit
... Monaco Editor with comprehensive ... Java programs efficiently Β· Full two-phase compilation: javac compiles your Java source code, then executes the compiled class...
🌐
Scaler
scaler.com β€Ί home β€Ί topics β€Ί java β€Ί java compiler online | write, run & debug java easily
Online Java Compiler
You can either create a new Java file by clicking on a "New File" or "Create" button or, if the platform allows, upload an existing Java file. Utilize the code editor provided to script your Java code.
Top answer
1 of 3
161

How do I load a Java Class that is not compiled?

You need to compile it first. This can be done programmatically with the javax.tools API. This only requires the JDK being installed at the local machine on top of JRE.

Here's a basic kickoff example (leaving obvious exception handling aside):

// Prepare source somehow.
String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"world\"); } }";

// Save source in .java file.
File root = Files.createTempDirectory("java").toFile();
File sourceFile = new File(root, "test/Test.java");
sourceFile.getParentFile().mkdirs();
Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));

// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());

// Load and instantiate compiled class.
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
Class<?> cls = Class.forName("test.Test", true, classLoader); // Should print "hello".
Object instance = cls.getDeclaredConstructor().newInstance(); // Should print "world".
System.out.println(instance); // Should print "test.Test@hashcode".

Which yields like

hello
world
test.Test@cafebabe

Further use would be more easy if those classes implements a certain interface which is already in the classpath.

SomeInterface instance = (SomeInterface) cls.getDeclaredConstructor().newInstance();

Otherwise you need to involve the Reflection API to access and invoke the (unknown) methods/fields.


That said and unrelated to the actual problem:

properties.load(new FileInputStream(new File("ClassName.properties")));

Letting java.io.File rely on current working directory is recipe for portability trouble. Don't do that. Put that file in classpath and use ClassLoader#getResourceAsStream() with a classpath-relative path.

properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("ClassName.properties"));
2 of 3
6

In the same vein as BalusC's answer, but a bit more automatic wrapper is here in this piece of code from my kilim distribution. https://github.com/kilim/kilim/blob/master/src/kilim/tools/Javac.java

It takes a list of strings containing Java source, extracts the package and public class/interface names and creates the corresponding directory/file hierarchy in a tmp directory. It then runs the java compiler on it, and returns a list of name,classfile pairs (the ClassInfo structure).

Help yourself to the code. It is MIT licensed.

🌐
Quora
quora.com β€Ί How-can-you-convert-java-files-to-class-files
How to convert .java files to .class files - Quora
IDEs (IntelliJ IDEA, Eclipse, NetBeans) compile automatically on build/save; exported artifacts include .class files in the project’s output/build folders. ... Gradle: gradle build or gradle classes (classes go to build/classes) These handle classpath, dependencies, and packaging. ... Install a JDK (OpenJDK or Oracle JDK). JRE alone does not include javac. As a lightweight alternative, use an online Java compiler or a cloud IDE (Replit, GitHub Codespaces) to compile and download .class files.
🌐
OneCompiler
onecompiler.com β€Ί java β€Ί 425g3j6q3
Create class and object - Java - OneCompiler
Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding. OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab.
🌐
Codiva
codiva.io
Superfast Online Java Compiler, Editer and IDE - Codiva.io - Compile and Run Java Online
Compile and run C, C++, Java programs online with Codiva.io. Used by CS teachers in the US, India, Phillipines, ... Compiles as you type with auto complete suggestions. It is fast, even on 2G mobile. Try now!
🌐
Replit
replit.com β€Ί languages β€Ί java10
Java Online Compiler & Interpreter - Replit
Code, collaborate, compile, run, share, and deploy Java and more online from your browser.Sign up to code in Java Β· Explore Multiplayer >_Collaborate in real-time with your friends Β· Explore Teams >_Code with your class or coworkers Β·
🌐
TutorialsPoint
tutorialspoint.com β€Ί online_java_compiler.php
Online Java Compiler & IDE - Free Java programming Tool
Free online Java Compiler and IDE. Write, compile, run and debug Java code online. No installation required. Supports debugging, code sharing, and multiple examples.
🌐
MyCompiler
mycompiler.io β€Ί online-java-compiler
Online Java Compiler - myCompiler
Online Java Compiler - Edit, Compile and Run your Java code with myCompiler IDE. Simple and easy to use IDE with built in support for JAVAC for compiling Java programs.
🌐
CodeChef
codechef.com β€Ί java-online-compiler
Online Java Compiler and Visualizer
Welcome to our AI-powered online Java compiler and interpreter, the perfect platform to run and test your Java code efficiently. Our tool makes coding easy for developers of any skill level, whether you're a beginner or experienced.
🌐
JDoodle
jdoodle.com
JDoodle - Online Compiler & IDE for 110+ Languages | Free
JDoodle is an Online Compiler, Editor, IDE for Java, C, C++, PHP, Perl, Python, Ruby and many more. You can run your programs on the fly online, and you can save and share them with others. Quick and Easy way to compile and run programs online.