Reputation: 29
Actually, I was excuting a perticular program, as shown in Picture below...
1st Case : Running the same.java file with default Run option in Intellij IDE
--> Gives the correct output ( you can see in pic )
error: cannot find symbol
anotherClass.main(new String[]{"aditya"});
^
symbol: variable anotherClass
location: class same
Why Is that Issue... As of I know In java by default the "java.lang" and the "current working directory" packages are by default imported...
Upvotes: 1
Views: 272
Reputation: 6178
When you run code from IDE, the IDE compiles all the classes in your project. When you run code from the command prompt, YOU must compile all the necessary classes. In this case, you didn't compile anotherClass
.
Here is a link on how to compile multiple files from command line: https://docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html. Go down to the section "Example of Compiling Multiple Source Files"
As a side note, use proper naming conventions. The industry-accepted naming convention for classes is "CamelCase".
Upvotes: 1
Reputation: 61
This is happening because the compiled byte code of file anotherClass.java isn't available at location com/company/com. Check the folder where IntelliJ keeps the .class files and execute them directly.
Additionally, if you want to compile these files manually via terminal, do it from outside the package (src folder).
Upvotes: 1