I'm working on a Java project that I've developed using IntelliJ, and though it runs fine within the IDE, I want to compile and run it via Windows PowerShell. My project is organized into multiple package folders located in the 'src' directory. The main method is in a file named 'Main.java' which is under the 'main' package.
I tried compiling the project from the 'src' directory using a few different commands. For example, I initially used 'javac .mainMain.java' but didn't like that all the .class files were generated in the same directory as their respective .java files. Then I attempted 'javac -d out .mainMain.java' to put the class files into an 'out' folder. Meanwhile, I updated to the latest JDK and set the necessary environment variables.
I also tried other commands like 'javac *.java', but my system rejected that as an invalid command. When attempting to run it after building, I encountered an error: 'Could not find or load main class Main'.
I've only been able to run my program by executing the uncompiled 'Main.java' file directly, which seems like a workaround rather than a proper solution. Can anyone help me understand how to correctly compile and run my project in PowerShell?
3 Answers
Just a thought, but IntelliJ's build system doesn’t necessarily have command-line support. You might have selected IntelliJ's own system when you created the project, so you might want to consider using Maven or Gradle instead—they're pretty solid command-line tools for builds.
IntelliJ has a play/run button for debugging, right? Make sure you've tried that inside the IDE first. But I totally get wanting to do it in PowerShell; it’s a good skill to have!
Yeah, it runs okay in IntelliJ, but I really want to get it working from the terminal. It just seems odd that I can't figure it out.
Have you tried using 'javac *.java'? That command might help compile all Java files at once. However, I understand it can be tricky sometimes with PowerShell!
Yes, I tried that, but the terminal said it doesn't recognize it, showing an error related to invalid filenames. Not sure what's up with that.
Oh, I see! Maybe I should look into using one of those for my project. Thanks for the suggestion!