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
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:This will create a
javaBasics.class
file in your directory.2. Run the compiled class:
Run the compiled Java program with the following command:
Note:
.java
extension when running the class. You only use the .java extension when compiling.javaBasics.java
exactly matches your class namejavaBasics
.If you still encounter issues, please share the error message, and I’ll help further!
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.
Without compile
java javaBasics.java
With compile
javac javaBasics.java
java javaBasics