Unable to compile Java class because the file isn't found - java

I have file under classes in tomcat...\webapps.....\classes named PropertyExample.java
in classes I have a folder called foo in which I have a class person.java
I am importing that person file in PropertyExample.java
and trying to compile PropertyExample.java this below but its showing an error
C:\>javac -cp .;"c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes" PropertyExample.java
javac: file not found: PropertyExample.java
Usage: javac <options> <source files>
use -help for a list of possible options

You execute javac the the folder C:\ (root folder). The .java file is somewhere else.
So it can't be found.
The command: C:\javac Someclass.java works IF and ONLY IF the file Someclass.java is in the folder C:\ you wrote that your file is in ...tomcat/webapps/classes (whatever) so you must do a cd to that dir before calling the javac. like this:
cd c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes
javac -cp . PropertyExample.java

The classpath tells javac where to find class files - not where to find source files. You need to give the path name to your source file (either relative or absolute). For example:
javac -cp "c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes"
"c:\Tomcat 6.0\webapps\jsp\WEB-INF\classes\PropertyExample.java"
Of course it would be easier just to change to that directory to start with... or better, to use an IDE or a build system like Ant...

Related

create java class file in another directory

say ~ is my home directory. it got a child directory java , which has two child directories classes and sources. Say I got a file in ~/java/sources/ names Graph.java. I want to compile it such that its class file ends up being in ~/java/classes/.
javac Graph.java -d "~/java/classes/"
Use -d parameter with javac to specify the directory you'd like to store the compiled class file.
As Ex:
javac -d <path where to store class file> <your java file to compile>

Create a bash alias to compile any .java program in the current directory

I am attempting to create a bash alias that compiles all files with the .java extension and then runs all the files with the .class extension in the current working directory.
I currently have the alias
alias jcompile="cd $pwd; javac *.java; java *.class"
I would expect this to find any java programs and compile them, and then run them, but it throws this error.
error: file not found: *.java
Usage: javac <options> <source files>
use --help for a list of possible options
Error: Could not find or load main class *.class
Caused by: java.lang.ClassNotFoundException: *.class
Unless you're defining $pwd specifically, it doesn't mean anything. pwd is a command that will tell you your current path, and $PWD is a variable that holds your current path. If you are defining a variable pwd, you might want to change it to something more distinctive.
If you want your function to run in a location that isn't your current directory, you could pass it as an argument:
jcompile() {
cd "$1"
javac *.java
java *.class
}
and run it via
jcompile /my/path/to/directory

Compiling multiple jar and java files using javac

I downloaded a sample code written in java that has multiple jar files and java files. I am not a Java programmer so I am having a hard time compiling the code. Here's my attempt:
javac -classpath lib/*.jar src/*.java
However this is what I get:
javac: invalid flag: lib/dom4j-1.6.1.jar
Usage: javac <options> <source files>
use -help for a list of possible options
What's wrong with my approach and how can I compile the code? ALl the jar files are located in the lib folder, and the java files in the src folder.
You need to stop the shell from globbing the wild-card in lib/*.jar by escaping it.
Also, you need to remove the .jar suffix ... because that's how classpath wildcards work; see Oracle's "Setting the classpath" document.
So ...
javac -classpath lib/\* src/*.java
Using an IDE is another option. However, if all you want to do is compile and run, then downloading and installing and learning to use an IDE is overkill (IMO). And the flipside is that it is good for an IDE-using Java programmer to also understand how to compile and run from the shell prompt ...
old post, but thought below details help,
you can specify jar files by separating by ; in windows and : in unix
Eg: (windows)
javac -cp first.jar;second.jar;third.jar YourClass.java
(unix)
javac -cp first.jar:second.jar:third.jar YourClass.java
Source: https://gullele.com/pass-all-the-jars-in-classpath-when-compiling-java/

Unable to get javac to automatically recompile a source file

I am experimenting with the javac command line options, in order to learn about the -sourcepath and -classpath options. When I run javac, having tried four different command line options for it, I am unable to obtain a recompiled .class file.
Here is my folder structure. Please note that due to testing, the Test.java file is located inside the "bat" folder, which is an admittedly odd location.
projects \ prj1 \ bat \ bat.bat
Test.java
Test.class <--- unable to obtain recompiled file.
src \ Main.java
Main.class
The contents of my two .java test files are:
// Main.java, located in the src folder
class Main {
public static void main(String[] args) {
new Test();
}
}
// Test.java, located in the bat folder
class Test {}
Regarding the execution of javac at the command prompt, here are four options that I have tried. I've run these commands from a batch file called bat.bat, which is located inside the "bat" folder.
javac ..\src\Main.java (no sourcepath, no cp)
javac -sourcepath . ..\src\Main.java (sourcepath)
javac -cp . ..\src\Main.java (cp)
javac -sourcepath . -cp . ..\src\Main.java (sourcepath, cp)
In all of these javac commands above, I am unable to obtain a recompiled .class file, for the Test.java file. Is this because I have not edited the Test.java file, since initially compiling it? Please note that I have no CLASSPATH environment variable set. Thanks.
If class files are already present in the destination / output folder, javac will only recompile the source java file if it has been modified since the date/time of the class file.
If you want to recompile the source files, then first delete the *.class files before calling javac.

Java will not compile properly from the command line "cannot find my source files"

I am trying to compile my source code from the Windows 7 command prompt. I have added the directory in which my *.java file is located to the classpath. Yet, I still get this error:
C:\Users\Alex>javac HelloThere.java
javac: file not found: HelloThere.java
Usage: javac <options> <source files>
use -help for a list of possible options
I'm very confused as to why this happens because if I navigate to the folder where this file is located, it will compile. However, this is not a satisfactory solution since I intend on compiling JUnit tests directly from the command line as well.
Other solutions I have attempted:
C:\Users\Alex>javac -classpath "C:\Users\Alex\AndroidProject\UnitTest\src" HelloThere.java
javac: file not found: HelloThere.java
Usage: javac <options> <source files>
use -help for a list of possible options
I do not think this has ANYTHING to do with typos.
Why can't I compile my project like this?
The CLASSPATH variable is not used by javac to find where your source code lives. Use the -sourcepath arg to javac instead.
Save yourself a lot of time and manual typing and use a build tool like Apache Ant or Maven.
You don't want to use classpath, that tells the compiler where to find externally referenced .class files used by your file. You want to use -sourcepath which tells javac where your .java files may be hiding.

Categories

Resources