Ask coding questions

← Back to all posts
How to change the file name without error?(java)
samjh8xie (0)

(I'm a new coder so I might not know a lot.) Basically, I changed the class from "public class Main" to "public class emails". I then changed the file name to "emails.java" too, but when I ran the code, it said "unable to run, main file "Main.java" not found." It kept on saying this after everything I tried. How can I stop this?
Also, whenever I add a new file to try to start over, it just prints the code from the Main.java file no matter what I do.

Answered by JacobMcPherson1 (237) [earned 5 cycles]
View Answer
Comments
hotnewtop
JacobMcPherson1 (237)

make a file called '.replit' and type run="javac emails.java && java emails" and that should fix it

samjh8xie (0)

@JacobMcPherson1 Thanks I appreciate it! Do I have to do this for every repl?

SixBeeps (5223)

To further elaborate on what @JacobMcPherson1 said, the reason why this happens is because Replit maps a console command to the run button for every language. By default, this command compiles a file named Main.java and then runs it. If you rename the already-existing Main.java file that comes with the Repl, that file will no longer exist, and the command will fail. By retting the command manually in the .replit file, you are then able to map a different file name to compile and run.

This is the best option here since then you won't have to type the commands each time you want to run the program, but it's certainly not your only option. Throughout APCSA, I would create a new Java file for the runner class instead of using Main, then execute the runner's main method from the Main class' main method.