vscode-java-debug icon indicating copy to clipboard operation
vscode-java-debug copied to clipboard

Could not find or load main class QuartsToGallons

Open zixuan8368 opened this issue 1 year ago • 2 comments

PS C:\Users\ZIXUAN8368\Desktop\Java\Chapter2> cd "c:\Users\ZIXUAN8368\Desktop\Java\Chapter2" ; if ($?) { javac QuartsToGallons.java } ; if ($?) { java QuartsToGallons } Error: Could not find or load main class QuartsToGallons Caused by: java.lang.NoClassDefFoundError: QuartsToGallons (wrong name: Chapter2/QuartsToGallons)

I am new to VS Code and i couldn't figure it out how to solve this, i will need some help

zixuan8368 avatar Oct 15 '24 14:10 zixuan8368

pls share the specific steps to reproduce your issue.

testforstephen avatar Oct 16 '24 02:10 testforstephen

Image

zixuan8368 avatar Oct 16 '24 14:10 zixuan8368

Hi @zixuan8368, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.

Solution

  • Verify whether your QuartsToGallons.java file declares a package. If it begins with
    package Chapter2;
    
    then you must:
    1. Compile from the folder above Chapter2:
      cd C:\Users\ZIXUAN8368\Desktop\Java
      javac Chapter2\QuartsToGallons.java
      
    2. Run it by its fully-qualified name:
      java Chapter2.QuartsToGallons
      
  • If you don’t intend to use packages, remove or comment out the package Chapter2; line and then recompile & run from within the same directory:
    cd C:\Users\ZIXUAN8368\Desktop\Java\Chapter2
    javac QuartsToGallons.java
    java QuartsToGallons
    

Other references with high confidence

  • Java does not execute unless package statement is commented out
    Demonstrates how a package declaration forces you to run the class using its package-qualified name (or from the correct parent folder).
    https://github.com/redhat-developer/vscode-java/issues/877

The team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!

github-actions[bot] avatar Nov 10 '25 11:11 github-actions[bot]