See this: YouTube video reference

Here, cmd works fine when working with Java, but not Powershell. Why is it so and how to make Powershell work with it?

Update (adding commands and outputs, so you may skip watching the video)


C:\Users\reesh\Desktop\Java>javac MyFirstJavaProgram.java

C:\Users\reesh\Desktop\Java>java MyFirstJavaProgram
Hello World



PS C:\Users\reesh\Desktop\Java> javac .\MyFirstJavaProgram.java
PS C:\Users\reesh\Desktop\Java> java .\MyFirstJavaProgram
Error: Could not find or load main class .\MyFirstJavaProgram
Caused by: java.lang.ClassNotFoundException: /\MyFirstJavaProgram
PS C:\Users\reesh\Desktop\Java>

In other words, PS is giving error, while cmd is not.

  • It is because powershell works fundamentally different. It would likely be .\javac "MyFirstJavaProgram.java" though – LPChip Dec 7 '17 at 21:06
  • Thank you. Solved my confusion. I am a newbie and I see people downvoting rather than giving answers. Very hostile. – Reeshabh Ranjan Dec 7 '17 at 21:09
  • 3
    Welcome to the internet, the place where some people are not friendly to others because they forget there are real people on the other side of the internet too. – LPChip Dec 7 '17 at 21:27

By default, Powershell has a security configuration that when you try to run a program by just giving the name, it does not search the current folder like cmd. Instead it just searches your PATH variable.

Therefore, the first fix is to make javac to ./javac. Secondly, when you write .\MyFirstJavaProgram, Java doesn't seem to be able to read the file name. Instead try making it a forward slash ./MyFirstJavaProgram.

Final command ./java.c ./MyFirstJavaProgram. Although, I want to emphasise this, you don't need a ./ on MyFirstJavaProgram because it is an argument, not a script or executable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.