How to set the output files when compiling with javac [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

Compiling java class from command line defined in a package [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I'm able to compile and run a simple hello world program from command line with
javac hello.java
java hello
however, if I had a package statement at the top package com.mypackage.myclass and try the same I get
Error: Could not find or load main class Reflection
What is exactly happening? And how do I fix it? Thanks.
UPDATE: Thanks everyone. I already had created the directory structure manually. In order for this to work I have to run java com.mypackage.myclass from the root directory, otherwise it will not work. Still don't understand the underlying mechanism. What is exactly happening?
If you have a java class like com.example.test.Main then the compiled class should be in a folder com\example\test and the class file name Main.class
You could manually do it, but compiler has a switch -d <target folder> which will create the folder structure for you.
javac -d . Main.java
In the above command the target folder is the current folder where you have the Java source file.
Once compiled you must use fully qualified name of the class to execute from the same folder you compiled the java file.
java com.example.test.Main
Screen Cap

Ambiguity with package statement in java

Consider the following code:
package com.a.b;
class Test {
public static void main(String[] args) {
System.out.println("Hello to the world of packages");
}
}
I've compiled the program like so:
javac Test.java
This created Test.class file in the current working directory. How should I run the program now? I tried:
java Test
but I'm getting a "No class def found error". If I compile this way:
javac -d . Test.java
It's creating directory structure, then I'm able to run through
java com.a.b.Test
If I can compile without directory hierarchy, why can't I execute?
In your first command:
javac Test.java
It compiles fine because there are no errors and places the Test.class file in the current directory, because that's where Test.java resides, as per the documentation:
If the -d option is not specified, then javac puts each class file in the same directory as the source file from which it was generated.
The source file is in the current directory so the class file is placed in the current directory. Now when you execute like so:
java Test
There's an error because you must use the fully qualified class name. You specified a package, and even though it's in the current directory, you still must use the fully qualified name to specify where it is. Without the full name, Java can't find it an execute it, thus the error. You can do the following:
java com.a.b.Test
Java will find it appropriately and all will execute fine.
Now, I would recommend using the -d option to correctly place your class files for organization. I would also recommend actually creating the packages you specify, so put your Test.java file in the directory ./com/a/b.
You must always run your Java program with the fully qualified class name:
It doesn't matter whether you compile it using the javac -d option.
So, in your case, it will be:
java com.a.b.Test
It is a best practice to compile it with the -d option and store .class files in the proper directory structure as per the package name.
Update
Also, ensure your current directory is in the class-path.
Try running this:
java -cp . com.a.b.Test
Update 2
When the -d option is not used with javac, the class files are created in the current directory. You will have to move/copy the class file(s) manually to the appropriate directory and then execute them. The -d option is a short cut of achieving this.
Java search for classes in classpath. By default classpath contains only currrent directory.
When Java search for class it iterates thru classpath element. If element is directory it search file with name of the class end extension .class in some subdirectory. To be precise this subderectory is found by replasing dots to directory separation simbol (/ or \ depending on your operation system) and resolving result.
1) Add your class to the java build-path
2) Run it with the full path.

Compiling a java file using javac and the command line

I am trying to learn more about javac and how to use developer tools for Java using the command line.
As far as I understood, the option -classpath is needed to specify the path where javac searches for our classes and resource files, if we are not in the current directory, because usually the class path is set to our current working directory.
This is my current working directory:
/Users/user1/Desktop
And I am trying to compile a .java file which is in:
/Users/user1/Desktop/PF/
and the file is called MainClass.java.
I am trying to compile it using the following command:
javac -classpath /PF MainClass.java
But it does not seem to work, in fact I keep receiving the following:
javac: file not found: MainClass.java
Usage: javac <options> <source files>
use -help for a list of possible options
What am I doing wrong?
Classpath is for .class files, not for .java files.
javac command needs correct path to the .java file to compile it. So
javac ./PF/MainClass.java
Will create the class file in current directory.
If your MainClass.java depends on any class files to compile correctly, then you put those class/jar files in classpath.
That isn't how the classpath works. You use the classpath to point to classes that your Java file needs in order to compile. You don't use the classpath to point to the Java file itself.
Either go into the PF directory and do this:
javac MainClass.java
That will create the MainClass.class file inside the PF directory. If instead you want to create the MainClass.class file on your desktop, then from your desktop, do this:
javac PF/MainClass.java
-classpath
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 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.
class path is used to specify the compiled sources that need to be used in your class. For example in this code if you are accessing another class then you should specify the location of the compiled sources of the that class.
In your case if don't have any class dependency then simply remove classpath option and compile using[navigate inside folder]
javac Mainclass.java
Remove the -classpath. And if you are in the place where the java file is required (which currently you arent) you can remove that PF/ too.

Running a java program with multiple jar files and classes [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc

Compiling a Jar with java [duplicate]

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 9 years ago.
For years I have been using an IDE (Eclipse) to compile my jar files for me, through the years I have learned about how they work however I still don't fully understand how the jar knows where the main method is, I am also curious about how simple (or not) it is to compile one manually.
I have a (sort of) IDE I'm working on that will need to be able to compile and run a jar that includes both the file from the user and either a jar or a bunch of other classes (the API), I have seen some questions here mentioning Java JavaCompiler class but never giving demo code and there seems to be a next to no one that knows how to compile manually so I would like to contribute. So, how can I create a jar file using java code? Please provide demo code.
I still don't fully understand how the jar knows where the main method is
That's the job of the manifest file.
I am also curious about how simple (or not) it is to compile one manually.
It's pretty straightforward - you use the jar tool after you've built the class files.
Let's do a full walk through.
Create a directory called src and a directory called bin. Under src, create a directory demo and a file called Test.java in that directory:
package demo;
public class Test {
public static void main(String[] args) {
System.out.println("Working!");
}
}
Now compile the code:
javac -d bin src/demo/Test.java
(That will work on both Unix and Windows.)
Then create a manifest file called manifest.txt - it doesn't matter where it goes really, but I'll just keep it in src for the moment:
Main-Class: demo.Test
Now build a jar file:
jar cfm test.jar src\manifest.txt -C bin demo/Test.class
And run it:
java -jar test.jar
These days you can specify the entry point on the command line instead of building a manifest file yourself:
jar cfe test.jar demo.Test -C bin demo/Test.class
See the linked docs for more details on how to use the jar tool, and the potential contents of the manifest.

Categories

Resources