skip to Main Content

I am learning Java and my code shows no problem but my terminal is not working. What should I do? It shows your classpath is not there.

I have tried watching some videos but it doesn’t work at all. It shows java basic.java is not on the class path. Only syntax errors are reported.

My code is:

import java.util.*;

public class javaBasics {

    public static void main(String args[] ) {
         int age = 22;
         if (age >= 18) {
             System.out.println("adult : drive, vote");
         } else {
            System.out.println("not adult");
         }
    }

My terminal is showing:

PS C:UsersachahOneDriveDesktopjava learningapp.java> javaBasics.java
javaBasics.java : The term 'javaBasics.java' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ javaBasics.java
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (javaBasics.java:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException 

2

Answers


  1. Follow the steps to resolve the issue:

    1. Compile the Java code:

    Before running, the javaBasics.java file should compiled first. In your terminal, navigate to the directory where your Java file is located and then use this command:

    javac javaBasics.java

    This will create a javaBasics.class file in your directory.

    2. Run the compiled class:

    Run the compiled Java program with the following command:

    java javaBasics

    Note:

    1. Don’t include the .java extension when running the class. You only use the .java extension when compiling.
    2. Ensure that your file name javaBasics.java exactly matches your class name javaBasics.

    If you still encounter issues, please share the error message, and I’ll help further!

    Login or Signup to reply.
  2. I hope your Java file name and class name are both the same.

    for eg. if the file name is basic.java then the class name is also the same – public class basic {...}

    You can run the code in two ways.

    1. Without compile

      java javaBasics.java

    2. With compile

      javac javaBasics.java

      java javaBasics

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search