CMD and Powershell work differently for building and running java files
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.
Related videos on Youtube
Reeshabh Ranjan
Updated on September 18, 2022Comments
-
Reeshabh Ranjan over 1 year
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)
CMD:
C:\Users\reesh\Desktop\Java>javac MyFirstJavaProgram.java C:\Users\reesh\Desktop\Java>java MyFirstJavaProgram Hello World C:\Users\reesh\Desktop\Java>
Powershell:
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.
-
LPChip over 6 yearsIt is because powershell works fundamentally different. It would likely be .\javac "MyFirstJavaProgram.java" though
-
Reeshabh Ranjan over 6 yearsThank you. Solved my confusion. I am a newbie and I see people downvoting rather than giving answers. Very hostile.
-
LPChip over 6 yearsWelcome 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.
-
devp over 3 years@Reeshabh by the way it was a good question it cleared my doubt also.
-