CMD and Powershell work differently for building and running java files

11,195

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.

Share:
11,195

Related videos on Youtube

Reeshabh Ranjan
Author by

Reeshabh Ranjan

Updated on September 18, 2022

Comments

  • Reeshabh Ranjan
    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
      LPChip over 6 years
      It is because powershell works fundamentally different. It would likely be .\javac "MyFirstJavaProgram.java" though
    • Reeshabh Ranjan
      Reeshabh Ranjan over 6 years
      Thank you. Solved my confusion. I am a newbie and I see people downvoting rather than giving answers. Very hostile.
    • LPChip
      LPChip over 6 years
      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.
    • devp
      devp over 3 years
      @Reeshabh by the way it was a good question it cleared my doubt also.