I am trying to compile a java file in command window.
For one file, I can run Javac command and it will generated a new temp.class file. but when try to run with Java temp, it will give me an error that could't find the class?
Any inputs is welcome!

Who is Participating?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
While setting the classpath explicitly is possibly the solution, the classpath should default to the current directory in your specific case (ie. "java temp") so that might not be the full solution.

Another likely possibility is that your java class "temp" has been declared to be in a package (look inside your file near the top for a line starting with "package"). If so, then you need to have that (and the resulting temp.class) in a directory structure that relates to the package name.

For example, say that your current directory is C:\Java and that your file looks something like the following...


import ...

public class temp ...

Open in new window

Therefore, you need to place in      C:\Java\my\java\package   (and javac should create temp.class there too) and then from the   C:\Java   directory, you would run     java       or        java -cp .
Radek BaranowskiFull-stack Java DeveloperCommented:
You need to specify local classpath by running

java -cp . Temp

Assuming that you are in a directory with Temp.class file
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.