How to run a Java program with several classes or packages in an online compiler
Several classes in one file run in any online Java compiler: the class with main starts the program. To keep each class in its own file, or in a package, send the other files with your program: OnlineCompiler.io's API takes Student.java beside your Main.java, or com/acme/model/Student.java in its package folder, and Java finds each class by its name, as it does on your own computer.
By the OnlineCompiler.io team. Updated .
Can I put several classes in one file?
Yes, and it is the quickest way. A Java file may hold many classes, as long as only one is public. Put the helper class above or below the one with main; here Student sits in the same file as Main:
class Student {
private final String name;
private final int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
String name() { return name; }
boolean passed() { return score >= 50; }
}
public class Main {
public static void main(String[] args) {
Student ada = new Student("Ada", 91);
Student tim = new Student("Tim", 42);
System.out.println(ada.name() + (ada.passed() ? " passed" : " failed"));
System.out.println(tim.name() + (tim.passed() ? " passed" : " failed"));
}
}
Ada passed
Tim failedThis is what the Java online compiler page runs: its editor holds one
file, so this is the way to use several classes there. Java's source launcher runs the file directly
(as java Main.java does), with no separate compile step.
Which class does the run start?
The one that declares public static void main(String[] args). Its position in the file does not matter, nor does its name: with a file that holds class Main first and class Student after, Main still runs. If no class declares main, the public class is the one named. Java does not run "the first class in the file" when another one holds main.
When a program has two classes with a main method, the program you send in code decides: only its main runs. A main in a file sent beside
it is never started, though that class is still available to the program.
How do I keep each class in its own file?
One class per file is how courses and IDEs lay out Java. Here Student gets its own file, and Main is the program:
public class Student {
private final String name;
private final int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String name() { return name; }
public boolean passed() { return score >= 50; }
}
public class Main {
public static void main(String[] args) {
Student ada = new Student("Ada", 91);
Student tim = new Student("Tim", 42);
System.out.println(ada.name() + (ada.passed() ? " passed" : " failed"));
System.out.println(tim.name() + (tim.passed() ? " passed" : " failed"));
}
}
To run it:
- Get a free API key (100,000 runs a month, no credit card).
- Put
Main.javain the request'scode: it is the program, and holdsmain. - Put each other class in
filesas anameand itscontent. The name is the class's name plus.java, capital letters included:Student.javaholdsclass Student. - Send it to
https://api.onlinecompiler.io/api/run-code-sync/with the compileropenjdk-25(OpenJDK 25).
curl -X POST https://api.onlinecompiler.io/api/run-code-sync/ \
-H "Authorization: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"compiler": "openjdk-25",
"code": "public class Main {\n public static void main(String[] args) {\n Student ada = new Student(\"Ada\", 91);\n Student tim = new Student(\"Tim\", 42);\n System.out.println(ada.name() + (ada.passed() ? \" passed\" : \" failed\"));\n System.out.println(tim.name() + (tim.passed() ? \" passed\" : \" failed\"));\n }\n}\n",
"files": [
{
"name": "Student.java",
"content": "public class Student {\n private final String name;\n private final int score;\n\n public Student(String name, int score) {\n this.name = name;\n this.score = score;\n }\n\n public String name() { return name; }\n\n public boolean passed() { return score >= 50; }\n}\n"
}
]
}'The response's output is what the program printed, and exit_code is 0:
Ada passed
Tim failedThere is no compile command to run for Student.java. When Main uses Student, Java looks for a file called Student.java next to the program and
compiles it for you. Add "input" to the request when the program reads from System.in.
How do I use packages, such as com.acme.model?
A package is a folder. A class declared with package com.acme.model; must be in com/acme/model/, and a file name in files may hold those folders: at most 4 parts,
file name included (the naming rules), so com/acme/model/Student.java is the deepest that fits. The program
below is in package com.acme and uses Student from com.acme.model:
package com.acme.model;
public class Student {
private final String name;
private final int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String name() { return name; }
public boolean passed() { return score >= 50; }
}
package com.acme;
import com.acme.model.Student;
public class Main {
public static void main(String[] args) {
Student ada = new Student("Ada", 91);
Student tim = new Student("Tim", 42);
System.out.println(ada.name() + (ada.passed() ? " passed" : " failed"));
System.out.println(tim.name() + (tim.passed() ? " passed" : " failed"));
}
}
curl -X POST https://api.onlinecompiler.io/api/run-code-sync/ \
-H "Authorization: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"compiler": "openjdk-25",
"code": "package com.acme;\n\nimport com.acme.model.Student;\n\npublic class Main {\n public static void main(String[] args) {\n Student ada = new Student(\"Ada\", 91);\n Student tim = new Student(\"Tim\", 42);\n System.out.println(ada.name() + (ada.passed() ? \" passed\" : \" failed\"));\n System.out.println(tim.name() + (tim.passed() ? \" passed\" : \" failed\"));\n }\n}\n",
"files": [
{
"name": "com/acme/model/Student.java",
"content": "package com.acme.model;\n\npublic class Student {\n private final String name;\n private final int score;\n\n public Student(String name, int score) {\n this.name = name;\n this.score = score;\n }\n\n public String name() { return name; }\n\n public boolean passed() { return score >= 50; }\n}\n"
}
]
}'Ada passed
Tim failedYou send the program as code, without a folder name: its package com.acme; line
tells the server to run it from com/acme/, as you would from the folder that holds com on your computer. You do not need a -cp or javac command.
Every other file goes in files under its package's folders.
How do I send my files from Python?
This script reads the program and the files you list from the folder it runs in, sends them and prints
the result. It needs the requests package (pip install requests).
# run.py: runs a Java program with its package folders. Run it in the folder that holds com/.
from pathlib import Path
import requests
# Every other file the program needs, with its folders.
files = ["com/acme/model/Student.java"]
response = requests.post(
"https://api.onlinecompiler.io/api/run-code-sync/",
headers={"Authorization": "YOUR_API_KEY"},
json={
"compiler": "openjdk-25",
"code": Path("com/acme/Main.java").read_text(),
"files": [{"name": name, "content": Path(name).read_text()} for name in files],
},
timeout=60,
)
if not response.ok:
raise SystemExit(f"HTTP {response.status_code}: {response.text}")
result = response.json()
print(result["output"], end="")
print(result["error"], end="")
print("exit code:", result["exit_code"])Ada passed
Tim failed
exit code: 0The official Python and Node.js clients take the same files list in their run method. The several source files docs list the rules for every language:
up to 20 files and 1 MB in total.
What do these Java errors mean?
Each error below was printed by a run on the service. Java prints the same cause once for every line that uses
the class, so the lists are long; the first lines tell you what is wrong. The program is saved under its
class name, which is why the messages say Main.java, and com/acme/Main.java in a package.
| Error | What it means | Fix |
|---|---|---|
cannot find symbol ... symbol: class Student | Java looked for Student.java and did not find it. The file was not sent, or its name differs from the class: student.java or Pupil.java are not Student.java. | Send the file in files, named after the class. |
package com.acme.model does not exist | The class is not in the folder its package names: here Student.java was sent at the top, or in com/acme/, instead of com/acme/model/. | Make the file's folders match its package line. |
file does not contain class com.acme.model.Student | The file is in the right folder, but its first line is not package com.acme.model; (or it is missing). | Add or correct the package line. |
Student is not public in com.acme.model; cannot be accessed from outside package | The class was written without public, so only its own package can use it. | Write public class Student, and public on the constructor and methods the other package calls. |
cannot find symbol on a class in another package, with no package error | The import line is missing, so Java looks for the class in the program's own package. | Add import com.acme.model.Student;. |
The first error, from running Main.java without the file Student.java:
Main.java:3: error: cannot find symbol
Student ada = new Student("Ada", 91);
^
symbol: class Student
location: class Main
...
4 errors
error: compilation failedStudent.java sent at the top while the program imports com.acme.model.Student:
com/acme/Main.java:3: error: package com.acme.model does not exist
import com.acme.model.Student;
^
com/acme/Main.java:7: error: cannot find symbol
Student ada = new Student("Ada", 91);
^
symbol: class Student
location: class Main
...
5 errors
error: compilation failedcom/acme/model/Student.java with its package line left out:
com/acme/Main.java:3: error: cannot access Student
import com.acme.model.Student;
^
bad source file: /sandbox/com/acme/model/Student.java
file does not contain class com.acme.model.Student
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
...
5 errors
error: compilation failedStudent declared without public and used from another package:
com/acme/Main.java:3: error: Student is not public in com.acme.model; cannot be accessed from outside package
import com.acme.model.Student;
^
...
5 errors
error: compilation failedWhich online Java compilers support several files?
If you want tabs for several files and a Run button in the browser, an online IDE with projects fits better than an API. This is how some tools handle several Java files:
| Tool | Several files and packages | What it does well |
|---|---|---|
| OnlineGDB | Files: yes, add or upload them to a project (its Q&A). In a 2024 answer about importing packages, the reply was that the code compiles on their server, so files must be uploaded to the project one by one. | A debugger in the browser. |
| One-file online compilers, including our Java page | Several classes in the one file; no separate files. | Quick runs with nothing to set up. |
| OnlineCompiler.io API | Yes: up to 20 files in files, in package folders. | Running the files from a script, a course platform or an autograder, in 46 languages. No multi-file editor in the browser. |
Can I grade a student's Student.java automatically?
Yes. The same files list works with run-tests, which runs the
program against inputs and expected outputs and marks each test passed or failed, so the student's class
and the Main you test it with travel in one request. The autograder
API page covers grading a whole class.
Frequently asked questions
Can I put several classes in one Java file?
Yes. A file can hold any number of classes, and only one of them may be public. Put the class with main last or first, as you like: the one that declares main is the one that runs. This works on the Java online compiler page, which holds one file.
How many files can I send with one run?
Up to 20 files and 1 MB in total, besides the program itself, which can be up to 100 KB. A run with files costs the same as any run, and the Free plan has 100,000 runs a month.
Which class does the run start?
The class in your program that declares public static void main(String[] args). If no class declares it, the public class. Java finds your other classes by their names, and by their package folders when they are in a package.
Can I use a package line in a single file?
Yes. A program that starts with package com.acme; is saved in com/acme/ and runs from there, as in an IDE. Classes of the same package in other files go in the same folder.
Do Maven, Gradle or jar files work?
No. The compiler runs your .java files with the Java standard library, with no Maven or Gradle, and programs have no network access to download jars.