Javac - Compile .java files in different directories - java

I'm creating a Submission System where I would like the Framework files in a fixed location on the server - a user will upload their file in a different directory.
If I'm just working with .class files, then the following command works when executed:
java -cp
/SubmissionSystem/Java/Assessment/Framework:/SubmissionSystem/Java/Assessment/Test
Assessment4
I was wondering whether there is a similar command that can be called to compile the .java files in different directories instead of .class files!
I'd appreciate any help!

Yes, just replace java with javac and instead of passing the folder with the sources to the -classpath option, pass it to -sourcepath:
javac -cp /SubmissionSystem/Java/Assessment/Framework -sourcepath "/SubmissionSystem/Java/Assessment/Test Assessment4"
Don't forget proper quoting when the paths contain spaces. That will compile the files in place (so they will mix with the .java source files). You can let javac write them to a new place using the option -d

Related

Recompiling using JAVAC command

this is the folderI am battling to convert classes2-dex2jar.jar.src.java.
So I deleted ONE DRIVE which was recommended. JDK-8u321 and JRE-8u321 are properly installed.
But whenever I say : javac classes2-dex2jar.jar.src.java it's saying javac: not a file. I also
tried javac classes2-dex2jar.jar.src.java\* still it's javac not a file.
this file folder contains many java sourcefiles
this is the command's response
I am unsure about the real situation.
Unzip classes2-dex2jar.jar.
It contains a sub-directory src/java/ with sources typically in a directory tree.
For every package there is a directory: package x.y.z giving x/y/z/.
You would need to call javac inside src/java listing all *.java
with src/java on the class path (-cp .).
(It might be easier to do this in an IDE.)

Compile java from multiple directories [duplicate]

This question already has answers here:
Java: How can I compile an entire directory structure of code ?
(10 answers)
Closed 7 years ago.
I'm trying to compile multiple .java programs from different directories either on Windows, Mac, or Linux... e.g. cmd or terminal. It doesn't matter.
However, I'm sure that many of you are familiar with how Netbeans stores files in different folders. I have been putting different concepts into different folders, and now I want to run all of them.
For example, a chess program that I have looks sort of like this:
/Chess
/build
/classes
/chess
/Chess.class
/color
/colorHelper.class
/game
/Board.class
/Game.class
/GameManager.class
/Player.class
... etc. (the rest of the directories with .class files)
/build.xml
/gameLog.txt
/manifest.mf
/nbproject
... (some xml and .properties files)
/src
/chess
/Chess.java
/color
/ColorHelper.java
/game
/Board.java
/Game.java
/GameManager.java
/Player.java
... etc. (the rest of the directories with .java files)
So, my question is, how can you use javac *.java (in /src probably) to compile all of the files (because otherwise I get a cannot find symbol error. Since I get a file not found when I run javac *.java in src, I am at a loss.
Thanks in advance,
Dylan
It won't compile in one go.Actually,your src directory doesn't contain any .java file,SO it won't be done in that way!
I am afraid that you'll have to do it by changing your path under src folder to do the same.
You'll have to perform for each chess,color,game,etc. directories to achieve the same.
So,change path at each run or go as advised in the comments to achieve compilation of all the java files.
OR
As proposed by David Ehrmann in your comment,you can do it by compiling in one go using javac $(find . -name '*.java').
It will compile all .java files under your present directory(src(.)).
javac *.java compiles all the Java files within the current directory. In your case, since you have different hierarchy of folders based on the package, you cannot achieve this in one go. At best you can run the command javac *.java on each folder.
You should try using an automated build system like maven or ant. It will allow you to build your project with a single command. It will even allow running unit tests, and packaging your application as a jar (or whatever package you prefer).
If you really don't want to use one of those you can still do it with a single (albeit lengthy) command. The *.java is just a wildcard in a file path. Its looking for any files in the current directory ending with .java. On linux you can try something like this find -name "*.java" | xargs javac. The first part is a find command that searches recursively for all files ending with .java. It this pipe's that list of files to xargs which adds them all as individual arguments to the java command.
That really isn't the best way. Again, try maven. Its pretty simple to set up for a basic project, your IDE usually has a feature to create a maven project for you as well.
to compile more than one file having different package name in same folder use
javac -d . *.java

How to include jar files with java file and compile in command prompt

I have 3 jar files and a .java file that depends on these jar files. How do I compile the .java file with these jar files using a command prompt?
You can include your jar files in the "javac" command using the "-cp" option.
javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java
Instead of "-cp" you could also use "-classpath"
javac -classpath ".:/home/path/mail.jar:/home/path/servlet.jar:" MyJavaFile.java
You could including the jars every time you compile by setting the environment variable "CLASSPATH" correctly. The environment variable will store the path where the jars and classes that needs to be used for compiling/executing any java file. You will not have to include the jars individually every time you compile you file.
Different machines have different methods to set the classpath as an environment variable.
The commands for Windows, Linux, etc are different.
You can find more details in this blog.
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Please try on Linux
javac -cp jarfile source file
EXAMPLE :-
javac -cp .:/jars/* com/template/*.java
Syntax will work on windows dos command:
javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java
The followings are steps,
Copy all jars and your .java file in a same folder (It will be easy to mention file names instead of mentioning long path. Though you can keep jar and .java in separate folders).
To compile,
javac -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>.java
To execute,
java -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>
I hope this helps!
Try to add all dependency jar files to your class path through environment variable settings or use the below steps:
Open command prompt.
Change directory to the location of you java
file that you would like compile.
Set the classpath for your dependency jar files as shown below:
set classpath=C:\Users\sarath_sivan\Desktop\jars\servlet-api.jar; C:\Users\sarath_sivan\Desktop\jars\spring-jdbc-3.0.2.RELEASE; C:\Users\sarath_sivan\Desktop\jars\spring-aop-3.0.2.RELEASE;
Now, you may compile your java file. (command: javac YourJavaFile.java)
Hope this will resolve your dependency issue.
This will create .class file:
javac -classpath "[jarname with specified path]" [java filename]
This will execute class file:
java -cp [jarname with specified path]: [java filename]
Try This.
javac -cp .:jars/jar1:jars/jar2:jars/jar3 com/source/*.java
javac -cp jars/jar1:jars/jar2:jars/jar3 abc.java
With -cp command we specify the path where to find the additional libraries which are required to compile the class. jar1, jar2 and jar3, available in jars folder are used to compile abc.java class.
You need to specify the dependencies in compile time as well as runtime
To compile use this format
javac -cp "*.jar;classfile_path" filename.java
Example:
javac -cp "ojdbc6.jar;c:\programs" Main.java
some times making following change works:
java -cp ".;%CLASSPATH%" classfilename
Note: ON Windows. For linux use $CLASSPATH instead.
If you are using Ubuntu:
/opt/JavaServices/sqlite $ export CLASSPATH=/opt/JarFiles/XXXX.jar:/opt/JarFiles/XXXX.jar:/opt/JavaServices/;javac SQLiteSample.java
Go to folder location (Out of package structure)
/opt/JavaServices $ export CLASSPATH=/opt/JarFiles/XXXXX.jar:/opt/JarFiles/XXXXX.jar:/opt/JavaServices/;java sqlite.SQLiteSample
Note: Please see the file locations and package names
Plenty of these answers helped me, but none that were exactly what I needed.
Assumptions:
Windows OS
JAR file and java file are in same directory
javac -cp <jar filename>.jar <filename>.java
java -cp <jar filename>.jar; <filename>
Keep in mind the syntax needs to exactly match. Cannot exclude file extensions or the semi colon.

javac not producing output

I am trying to compile a java project from the command line using javac but I am getting no output on the command line. Also, the .class files are not being generated.
The exact syntax I am using to invoke the javac command is below:
javac #options #javaFiles
The contents of the options file is below:
-g
-verbose
-d classes
-classpath cp1;cp2;cp3
where cp1, 2, 3 etc refer to jar files on my machine. The javaFiles file has a list of java files that I wish to compile:
C:\path\to\dir\one.java
C:\path\to\dir\two.java
The problem is that there is no output on the command line (If I miss-spell one of the command line switches then there is error output on the command line) and no .class files are generated in the classes folder.
Also, if I add the '-Xstdout output.txt' switch to the options files and try and compile. The javac command, exits and the output.txt fiel is created but there is no information in the file.
My question, is:
Am I using the wrong syntax to invoke javac and how should I change this?
EDIT:
I changed the options and javaFiles files as mentioned in Jasper's answer, but am getting the
below error now:
*__* javac #options #javaFiles
javac: file not found: com\compname\cloud\automation\portal\definitions\landing\LandingPage.java
Usage: javac <options> <source files>
use -help for a list of possible options
EDIT 2:
Ok, this issue was with one of the jars on the classpath. When that jar was removed (pain in the a$$ removing each jar at a time) everything compiles correctly and the .class files are created.
I'm still not sure why javac fails silently when this jar is on the classpath, even with the verbose switch being used.
For the moment, I can get around not using this jar. I appreciate the help people have provided...
In the second file, instead of using full pathnames, use only the pathnames from the base directory of the package. Suppose your code is in a package org.mypackage, and your source files are in the directory C:\myproject\src\org\mypackage, put this in the second file:
org\mypackage\One.java
org\mypackage\Two.java
In the first file, add a -sourcepath option:
-sourcepath C:\myproject\src
I just ran into the same problem, and it seems related to signed vs. unsigned third-party jar files:
if I call javac and use unsigned jar files, everything works as expected
if I sign the third-party jar files before compiling my code, javac fails silently
Hope this helps,
.Andrea

Why this javac: File Not Found error?

Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java
and I have .java files in home/pathToFolderA/src
and home/pathToFolderB/gen-java
When I run javac and java in home/pathToFolderA/src everything works perfectly
But when I run javac from within home/pathToFolderB/gen-java on fileName.java I get a file not found error, specifically
javac: file Not found: fileName.java
Usage: javac <options> <source files>
Why could this be happening?
Thanks for all help
The classpath is used to find class files, not source files. (Nor is it used to find the java and javac binaries; those are found in your normal path.) You need to specify the files to compile explicitly:
javac /home/pathToFolderA/src/fileName.java
Obviously if you're already in /home/pathToFolderA/src then you can just use fileName.java because that's treated as being relative to your current directory.
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
Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java
That's wrong. The classpath is used to find *.class files, not operating system specific executables. The bin directory of your JDK does not belong in the classpath. Note that the classpath is also not for finding *.java source files.
When you run javac you need to specify the path to the source file, if it isn't in the current directory.
make sure that your file name contain no spaces
Eg:
HelloWorld.java
usually the errors occur when you rename the file by copy past that will cause a space between the name and the dot (this is the mistake:HelloWorld .java).
and make sure you changed the directory to the same folder your file in
Without a listing of the directory "gen-java" and the exact command you're typing,my guess would be that you're trying to compile a file that doesn't exist. Linux is case sensitive, so maybe that's your problem. Or the file doesn't exist.

Categories