javac

Member_2_3676181
Member_2_3676181 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Radek BaranowskiFull-stack Java Developer

Commented:
You need to specify local classpath by running

java -cp . Temp

Assuming that you are in a directory with Temp.class file
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial