Aditya Vaste
Aditya Vaste

Reputation: 29

Difference in Compilation output of Java program, When trying to run with 'Intellij IDE' and 'Terminal'

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 )

enter image description here

2nd Case : Running the same.java file with command javac same.java in Terminal

error: cannot find symbol
        anotherClass.main(new String[]{"aditya"});  
        ^  
  symbol:   variable anotherClass 
  location: class same

enter image description here

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

Answers (2)

hfontanez
hfontanez

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

Pratyush Choudhary
Pratyush Choudhary

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

Related Questions