How and where java compier stores .class file [duplicate] - java

This question already has answers here:
How to compile a java project with a terminal/cmd
(4 answers)
Closed 6 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
My previous question asked how to compile files with the command JAVAC. I still don't know how to set the output files of the compiled source files.

The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)
If you use the -d command line option, javac will also use the package of the class to generate a directory hierarchy, with the -d option specifying the root. Otherwise, each class file will be output in the same directory as the source file from which it was compiled.

From man javac on my system:
-d directory
Sets the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d /home/myclasses and the class is called com.mypackage.MyClass, then the class file is called /home/myclasses/com/mypackage/MyClass.class. If -d is not specified, javac puts the class file in the same directory as the source file. Note: The directory specified by -d is not automatically added to your user class path.
Hope that helps.

Related

Are jar files decompressed on the fly [duplicate]

This question already has answers here:
How does a jar file get executed? Do the classes get extracted somewhere?
(2 answers)
Closed 3 years ago.
Take a standalone executable jar file for example, in which we generate from our application with all the dependencies etc. My understanding is that this file contains all the classes etc. compressed.
When we execute this jar file via the command line as follows java -jar myjar.jar , is this being decompressed on the fly? Does the interpreter first decompress everything before executing or how does this work exactly?
We already have one answer to similar question here :
How does a jar file get executed? Do the classes get extracted somewhere?
The JVM is capable of loading classes or files from a jar file without extracting the jar to temp files.This functionality is also available to you in the standard library, see the JarFile for more information.
So no, the JVM does not extract a jar to temp files, classes (and resources) are simply loaded on demand.
We can also check if the jar gets extracted or not by executing the command "java -jar myjar.jar" and check the folder where jar is located if there is any extraction while executing the program.

Running Java command including all JARs in current folder

I have just shifted back from an IDE to Notepad to write a Java program. The program is using 20 JARs. I compiled successfully. When I decided to run the Java class file using
java -cp ".\\*" MyProgram
it was giving the standard error "Couldn't find or load main class....".
I was confused because when I used to run the java command with all files in an existing folder, it would just get those JARs as the current folder is already in the classpath. As the program is running from the current folder, I tried using -cp "." to include it explicitly in the classpath but that didn't work either.
Finally I was able to run the program with this command:
java -cp ".\\*;." MyProgram.java
I am asking this question to understand the actual logic behind Java's classpath.
Correct me if I am wrong, but I think that the JAR is just a standard archive in which all the packages are encapsulated in respective folders. If all the JARs are in my current folder including my main class file then why can't I run it with:
java -cp "." MyProgram
or simply:
java MyProgram
If the problem is with the multiple JAR files to include and that's why we used ".\\*" to include all the JARs in the classpath, then why do we have to explicitly include the current folder again in the classpath using:
java ".\\*;." MyProgram
To include all jar required to run your program in command prompt use wildcard *:
java -classpath E:\lib\* HelloWorld
You are using "." that defines current directory and "./*" defines all files in current directory.
The class path is a list of jar files and directories containing the classes and resources of your program. Mentioning a jar file adds its contents to the class path.
"./*" will get you only the jar files in the current directory, "." adds the current directory to the class path. This allows to access all classes (and the jar files as raw file resources but not its contents, i.e. the classes contained in them).
If you need both in the class path, you have to specify both.
You've answered your own question, sort of.
. means that it will look for .class files in the current directory.
JARs act just like a directory. So to have the abc.jar "directory" you would specify abc.jar in your classpath.
If you need both the .class files present in the current directory, and the .class files packaged into JARs found in the current directory, you would have the following classpath: -cp ".:*.jar
All the answers here are telling you to use the wildcard without extension (* or ./*) but this is a bad practice, you don't want Java to go look into irrelevant files, so specify the extension: *.jar.
"." means current directory not files in the directory
"./*" means all files in current directory.
So you want to use all jars in current directory so 2nd will work

Why do I have to specify Java classpath for my .class file when I am at the same folder in windows CMD? [duplicate]

This question already has an answer here:
classpath does not default to current directory
(1 answer)
Closed 6 years ago.
I have a simple "test.java" in a folder. I am under that folder that contains and "test.java" already ran "javac test.java". A "test.class" file is created.
But when I try to run "javac test", my environment always returns
Error: Could not find or load main class test
unless I type java -cp . test to get java executing the test.class file.
Why?
Thanks.
You answered your question itself.
When you do
java test
Then the current directory is not in your classpath.
Thus it works when you go with -cp . (as that says: add "." aka the "current directory to the current classpath)
On the other hand, by default "." should be in the default classpath ( see here ).
So, two explanations:
As said, some subtle hickup around your file names
Your environment actually sets a classpath ENV variable; and that one excludes "."

Does the javac command automatically create directories specified in the package declaration?

I've always used an IDE when working with Java so my knowledge on the javac command isn't that great. I want to know this: Does java generate the directories where the .class files should be placed in as specified in the .java files package declarations? Let me clarify, say you have a simple .java file like this on your Desktop:
package com.deangrobler.test
public class Test {
// ...
}
When running the following from your Desktop:
javac Test.java
Will it then automatically go and create the com/deangrobler/test directories and place the Test.class file therein?
From the docs --> http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
See the option section.
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d C:\myclasses and the class is called com.mypackage.MyClass, then the class file is called C:\myclasses\com\mypackage\MyClass.class.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.
Will it then automatically go and create the com/deangrobler/test
directories and place the Test.class file therein?
No. Even if you can use -d to specify where you want to place your class file created after compilation but java won't create package structure for you.
So,Unless you specify the -d option the compiler places each class file in the same directory as the corresponding source file was located.
Even if you have specified package name but you hadn't created that structure don't expect that from javac all the things you have to do manually.
For Example:
javac -d C:/tempFolder MyProgram.java//Will place .class file in to C:/tempFolder
//Only if C:/tempFolder is available
com
|
|---deangrobler
|
|-------test
|
|---Test.class//Places class file here after compilation
//Default place
If we use below command , it will create package structure from current directory.
javac -d . YourClassName.java
If we want to put your class files and its packages some other folder, use below command.
javac -d "d:/java_eg/" YourClassName.java
it will create package directories and class files inside java_eg folder inside the D drive.

automatically compiling multiple java packages [duplicate]

This question already has answers here:
javac option to compile all java files under a given directory recursively
(10 answers)
Closed 9 years ago.
I'm trying to compile a Java project from the command line. The project contains class files in different packages. The program compiles and run fine if I specify every java file of every package. Here's my directory structure:
toplevel/
mainFile.java
level1/ (Contains fileA.java)
Now if I do javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java, this compiles fine and I can run it with java toplevel/mainFile.
Now if a create a new folder called 'level2' in level1 and create a class 'B' inside it, the new directory structure becomes:
toplevel/
mainFile.java
level1/ (Contains fileA.java)
level2/ (Contains fileB.java)
To compile this I have to do:
javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java toplevel/level1/level2/fileB.java which is becoming ridiculous. Is there an instruction that recursively compiles each package and the files inside it?
Have you tried -sourcepath flag. From the Oracle documentation,
-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 semicolons (;) 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.

Categories