javac

Hi,
I am trying to compile a java file in command window.
For one file temp.java, 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!

Thanks
chenjhuaAsked:
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 temp.java file near the top for a line starting with "package"). If so, then you need to have that temp.java (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 temp.java file looks something like the following...

package my.java.package;

import ...

public class temp ...

Open in new window


Therefore, you need to place temp.java 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 my.java.package.temp       or        java -cp . my.java.package.temp
0
 
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
0
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.