1

I am very new to JAVA. I have written simple program (in Linux -VIM editor), compiled and executed it, everything is fine.

Now,I have moved that file to a different directory and am trying to compile(javac Myfile.java) it, but it throws an error message as javac-not found.

Can somebody explain what is the problem?

2
  • yes,I guess...because when I simply create a java file and compile it with javac myfile.java- its compiling fine Commented Mar 21, 2011 at 12:15
  • Classpath, not properly set. Check it. Commented Mar 21, 2011 at 12:39

2 Answers 2

1

Your original question was not totally clear (since it did not contain the complete error message).

From your comment:

$ javac Example1.java
javac: file not found: Example1.java
Usage: javac <options> <source files> use -help for a list of possible options

So, javac did not find your file example java.

Normally, you should not have to set the CLASSPATH (use export CLASSPATH= in bash), and javac would search the source in the current directory. Is your Example1.java in the current directory? (Type ls and look at the output.)

If not, you should give the path to this file to javac as a parameter ... but it really is better so simply move to the right directory with cd.

If you are using packages, position your shell to the directory on top of the package directory hierarchy, and call the compiler with the relative filename from there.


Edit, since I see the next questions coming:

  • The compiler will put the resulting class files in the output directory tree given by the -d parameter (or the current directory, if not given), by their package structure, so make sure you search them there later (when invoking the program).
  • If the compiler needs other classes to compiler the files indicated in the command line, it searches class files in the classpath (given by the -classpath or -cp option, or by the CLASSPATH environment variable, or the current directory) and source files in the sourcepath (given by the -sourcepath option or the classpath if no sourcepath is set). If for a needed both exist and the source file is newer, it is recompiled too. (They are searched according to the package-structure, too.)

    So in this case you should make sure to pass the -sourcepath option so the compiler can find your other source files.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes,actually I gave a try with that and it worked. like javac /home/kiddo/myfikle.java
1

set the classpath and path properly and check whether its working fine.

USAGE:
SET CLASSPATH=%<CLASSPATH>%
SET PATH=%<PATH_WHERE_JDKS_BIN_LOCATED>%

The path environment variable must point to the bin directory in the jdk installation...

USAGE:
Variable : JAVA_HOME
Value : C:\Program Files\Java\jdk1.5.0\bin;.
Variable: PATH
Value : C:\Program Files\Java\jdk1.5.0\lib

System Variables :
Variable : PATH (This will be there already)
Value : %JAVA_HOME%\bin;

Since the file is not in current directory do the below at the prompt
$cd home/kiddosr/Kiddo/Java_Programs/ and press enter
home/kiddosr/Kiddo/Java_Programs at this point of time type javac Example1.java

5 Comments

<CLASSPATH> - should be the directory where my file resides..? for example /home/kiddo/Java_pgms
SET the path correctly to bin directory of jdk installation and it should work
FYI..am using Linux $ PATH=/home/jdk1.6.0_14/bin $ java -version java version "1.6.0_14" Java(TM) SE Runtime Environment (build 1.6.0_14-b08) Java HotSpot(TM) Server VM (build 14.0-b16, mixed mode) $ CLASSPATH=/home/kiddosr/Kiddo/Java_Programs $ javac Example1.java javac: file not found: Example1.java Usage: javac <options> <source files> use -help for a list of possible options
just ignore the CLASSPATH and give it a try.
@kiddo: such information better put in your question (there is an edit link) than into a comment, it is better readable there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.