I'm trying to compile a servlet called BeerSelect.java and I'm getting this error:
javac: file not found: BeerSelect.java
I compiled using: javac -classpath "C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.34\lib\servlet-api.jar"; -d classes \BeerSelect.java
I used this command to compile with my current directory set to where the servlet is stored, my class path is set correctly.
I checked many related questions on this site and cannot get the answer
You shouldn't set your classpath to point to your JDK bin directory -- instead it should be the PATH environment variable, which serves a different purpose to classpath. (The classpath defines a list of jars and directories containing compiled Java .class code; the PATH variable defines a list of paths where the shell needs to look and locate programs to execute when they are not found in the current directory -- so if you type for instance zip -- it would look in all the directories defined in PATH and figure out that zip program is located under /usr/bin) Secondly if you want to compile sources from both directory you need to specify:
all the paths where the sources are (both home/pathToFolderA/src and home/pathToFolderB/gen-java)
the path where the compiled .class files to be generated
specify in the classpath any library you might use in your source files
To sum it up, it would be something like this to compile:
javac -d /home/pathToFolderWithResultsOfCompilation -classpath /path/to/some.jar:/path/to/another.jar home/pathToFolderA/src/*.java home/pathToFolderB/gen-java/*.java
and to run your compiled programs:
java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.Java
The problem with the command i was using:
javac -classpath "C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.34\lib\servlet-api.jar"; -d classes \BeerSelect.java
is with the path name to store the results of compilation (.class file) and putting a backslash on the BeerSelect servlet name (i highlighted the errors).
I refined the command to look like this:
javac -d C:\Users\ModernWarFare\Desktop\MyProject\beerV1\classes BeerSelect.java
the highlited path is where i'm going to store the .class file and i didn't specify the path to the BeerSelect.java file because it is in the current directory
note that i ommited the path to the servlet-api.jar file because i am using tomcat 7, it already have all the jar files i need stored on C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.34\lib directory.
Sorry for responding late, i have limited time online
Related
I have new laptop on which I have installed jdk1.8.0_91 and jre1.8.0_91.
Both are in the "C:\Program Files\Java" folder.
I have NOT set any classpath or any environment variables.
I wrote a a HelloWorld.java program and saved it in "C:\my Data" folder.
I then went to Command Prompt using cmd.
Then I changed the current directory to "C:\Program Files\Java\jdk1.8.0_91\bin" ..since here is the javac.exe
and then tried to compile my HelloWorld program and its giving the following error -
C:\Program Files\Java\jdk1.8.0_91\bin>javac -sourcepath C:\my Data\HelloWorld.java
javac: invalid flag: Data\HelloWorld.com
Usage: javac <options> <source files>
use -help for a list of possible options
I am not sure whether I am correctly using the "sourcepath" or not...
How should I tell the compiler where my source file is ?(and I want to resolve this without setting any classpath or any environment variables)
Use this instead...
javac -sourcepath "C:\my Data" "C:\my Data\HelloWorld.java"
The sourcepath parameter allows you to specify the DIRECTORY where source files will be found. As per the javac command line output:
-sourcepath Specify where to find input source files
The parameter after that specifies the actual Java files to compile. You will need " around the parameters, given that your paths have spaces in them. Avoid spaces in your paths where ever possible to avoid this issue.
-sourcepath is a PATH, you are giving a file name that's not a java file, that's not valid. From the docs:
-sourcepath sourcepath
Specify the source code path to search for class or interface
definitions. As with the user class path, source path entries are
separated by colons (:) and can be directories, JAR archives, or ZIP
archives. If packages are used, the local path name within the
directory or archive must reflect the package name.
[EDIT: OP changes the file name to .java in the question, as the other answer noted, it needed quotes.]
You need to place the source path in quotes so that the command line processes it as a single argument. The source path must also be the directory in your case, not the file:
javac -sourcepath "C:\my Data"
Path C:\my Data\HelloWorld.java has space in it hence the error.
Please enclose path in "" (double quotes)
I have trying to compile java files at the windows command line using commands such as:
java myProg once I have used javac to create class files.
Problems arise when I use packages with a number of source files.
Often but not always I get main not found errors even though a main exists.
I am not quite sure what some of the directives mean and that is why it seems hit or miss.
Question
what does -cp mean exactly? java -cp src\myDirectory.myfile
sometimes I see:
./ infront of source eg .\src\myDirectory.myfile
on other sites I have found
% javac -cp .;stdlib.jar MyProgram.java
% java -cp .;stdlib.jar MyProgram
while compiling a jar library with java source files
what doesthe ".;" mean?
basically how do I compile three java source java files in one package at the windows command line and what does -cp and .; mean?
-cp means class path if I'm not mistaken.
try reading the following java docs
-classpath path
Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:
javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
Answering your question directly, -cp means classpath or path.
Details on commandline arguments used while compiling and running a Java application can be found here: javac - Java programming language compiler
Extracting the description of -cp from that page:
-cp path or -classpath path:
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
. means the current directory.
To compile multiple files in a directory use the following:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
There are also a bunch of other related questions that should help you resolve this:
How to run a java program from the command line
How do I run java program with multiple classes from cmd?
Problems running a java program from the command line interface
Can't run multiple-class program from command line using packages
I am trying to compile a Java program (CallableStatementEx1.java), but when I try to compile it it gives error in command prompt:
C:\Windows\system32>set path = C:\Program Files\Java\jdk1.8.0_31\bin;
C:\Windows\system32>set classpath = C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\ojdbc14.jar;
C:\Windows\system32>javac CallableStatementEx1.java
javac: file not found:CallableStatementExt1.java
Usage: javac <options> <source files>
use -help for a list of possible options"
Here is what I have done:
Installed JDK 8 and Tomcat 7.0
Copied ojdbc14.jar in lib folder of tomcat
Changed value of "Path" in user variables to the JDK <bin> folder in environment variables
Tried to compile the program directly without referring type4 driver as class (didn't work , the file didn't compile... it needs some libraries from the type4 driver
Created a batch file to compile, written as follows:
set path = C:\Program Files\Java\jdk1.8.0_31\bin;
set classpath = C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\ojdbc14.jar;
javac CallableStatementEx1.java
java CallableStatementEx1
pause
but I still get the error shown above.
javac: file not found:CallableStatementExt1.java means the java compiler was not able to find the file CallableStatementExt1.java, which is the java file you intend to compile. Most likely the issue is, that your current directory is C:\Windows\system32 and should be the directory, that contains the .java file instead. You could also just give the full path of the file to javac instead, but I strongly suggest you put no .class, .java and no batch files in C:\Windows\system32, as this is a special directory for windows.
I can run java in cygwin+windows using the following settings (the sw/jar directory has several jar files, and I pick the relevant one from the java command line):
CLASSPATH=.;C:\sw\java_6u35\lib\\*;C:\sw\jar\\*
java org.antlr.Tool Calc.g
But I am having the following problems when running in linux:
(1) I can't set a directory name in a classpath, the following line reports an error:
setenv CLASSPATH .:/sw/jdk1.6.0_35/lib/\*:/sw/jar/*
(2) when I run explictly with -jar option, I still get an error:
java -jar /sw/jar/antlr-3.4.jar org.antlr.Tool Calc.g
error(7): cannot find or open file: org.antlr.Tool
However, the class does exist. When I do jar tf /sw/jar/antlr-3.4.jar, I get:
...
org/antlr/Tool.class
So my question is: (a) how do I specify in unix that my jar-directory is xxx that contains several jar files, and (2) how do I pick the relevant jar from this dir at runtime?
To specify multiple jars in a directory, directly in the java command, use this
java -cp "/sw/jar/*" org.antlr.Tool Calc.g
This will include all the jars
If you want to set the classpath in Unix/Linux systems, use this
export CLASSPATH=/sw/jar/a.jar:/sw/jar/b.jar
in unix use this to set the classpath:
export CLASSPATH=myClassPath
about not finding your jar, you're using a leading slash (/), that means that you path is absolute (not relative to your home folder) is this what you want?
if you want the path to be relative to your folder try:
java -jar ~/mypathToMyJar
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?
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.
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