Compiling a java file using javac and the command line - java

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.

Related

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.

Java run without jar

I wonder if it's possible to run a program without packaging it into a jar.
For instance we have this:
-AppRoot
Main.class
-Misc
Math.class
OtherTools.class
-YetAnotherFolder
UsefulFunctions.class
Is this possible? The main method should be executed from command line or similar.
You can run this way from the AppRoot directory
javac -cp Misc/*:YetAnotherFolder/* Main.java //To compile
java -cp Misc/*:YetAnotherFolder/* Main // To run
Below is some documentation
-classpath classpath
-cp classpath
Specifies a list of directories, JAR archives, and ZIP archives to search for class files. Class
path entries are separated by colons (:). Specifying -classpath or -cp overrides any setting of the
CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the cur-
rent directory (.).
Add all (sub-)directories containing class files to classpath and use the class with the main method as argument of the java executable.
The directory structure is your package structure.
java -cp ./:./AppRoot:./AppRoot/Misc:./AppRoot/YetAnotherFolder AppRoot.Main
This should work if all dependencies are resolved and on the classpath.

Is it possible to use relative path when compiling the java files?

Hi I am trying to learn to compile a java class with make file. My make file looks like this:
build:
javac test_java.java
clean:
rm -rfv *~ test_java.class
run:
java test_java
Now I have moved the test_java.java into a folder, called classes I am trying to compile the file using a relative path, is it possible?
I have tried:
javac -d classes test_java.java
but I am getting errors:
javac -d classes test_java.java
javac: file not found: test_java.java
Usage: javac <options> <source files>
use -help for a list of possible options
I have also tried: -d ./classes and also -d /home/the/whole/path (but I would like to have the relative path), and the errors are the same. It seems to work only the
javac classes/test_java.java
What am I doing wrong?
Update: The original answer here was completely wrong. You must specify the path to your source files. -sourcepath is for another purpose:
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.
Note that there are many other build tools for Java applications that are very mature and well regarded. Maven, Gradle, and Ant are the ones that spring to mind immediately. If you don't have to use make I would take a look at one of those.

Package not found; javac

This is annoying.
I have a directory structure like this
-lib
--some jar files
-packageName
--Main.java
--SomeOtherPackage
--SomeOtherJavaClass.java
Main.java imports SomeOtherPackage. And both java files uses jars in the lib.
What I do is add the jar files independently in the CLASSPATH. And then run as:
javac packageName/Main.java
but it gives the error that Package not found SomeOtherPackage . Shouldn't it automatically realize the dependency and build SomeOtherPackage as well? What would be the javac command and the classpath for the above case?
Thanks
The normal practice is to add the package root to the classpath.
When you're already in the package root, use -cp .. E.g.
cd /path/to/all/packages
javac -cp . packageName/Main.java
If you want to include JAR files as well, use the ; (or in *nix, the :) as classpath path separator:
javac -cp .;lib/file.jar packageName/Main.java
To save the time in repeating all the typing of shell commands, use a .bat (or in *nix a .sh) file. Or just an IDE if you're already familiar with java/javac and so on.
You need to add packageName to the CLASSPATH so it can find SomeOtherPackage

Making java packages

My Java classes organization has gotten a little messy so I'm going over something I've skipped in my Java learning: the classpath. I can't quiet get beloved classes to compile in the packages I have created for them. Here's my folder hierarchy:
.
com/
david/
Greet.java
greeter/
SayHello.java
SayGoodbye.java
Where SayHello's constructor just prints "hello" and SayGoodbye's prints "Goodbye" and Greet's main method just creates those two objects. At the top of SayHello is package com.david.greeter; and likewise with SayGoodbye and Greet's is package com.david;
In the greeter folder I was able to compile both java files but if I go to the current directory (the directory that holds com) and do javac -cp "com.david.greeter.*" com/david/Greet.java it says it can't find the classes as well as saying package com.david.greeter doesn't exist. I've also tried setting the $CLASSPATH manually.
I'm at my wit's end here, Stackoverflow (as I normally am when I post here). Do any of you know what I am doing wrong?
The java compiler will traverse the sub-directories of the classpath looking for the packages it needs.
So, your command line should be as follows:
javac -cp "." com/david/Greet.java
When the compiler sees a reference to com.david.greeter.SayHello while compiling Greet.java it will start with the directory in the classpath and traverse the hierarchy looking for the package it needs.
First, as documented in Setting the Classpath, the way you're currently setting your class path is wrong. Class path entries should be filename or directory. So using com.david.greeter.* doesn't make any sense. Second, the current directory is in the class path by default:
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
So if you execute javac (here is the man page by the way) from the folder containing com, you don't have to tweak anything, just type:
javac com/david/Greet.java
And javac will go through the directory tree to find references (e.g. SayHello if you're using it from Greet) and compile them too.
And by the way, if you have to set the class path, don't use the $CLASSPATH environment variable, this is just a bad practice in most case, prefer the -cp option.
If you are in the folder containing com, then try this:
javac -cp . com\david\Greet.java
This is incorrect (as the compiler has already told you):
javac -cp "com.david.greeter.*
Open a command shell and navigate to the directory that contains the "com" directory.
I think you really want this to compile SayHello.java and SayGoodbye.java:
javac -cp . com/david/greeter/*.java
This to compile Greet.java:
javac -cp . com/david/*.java
And this to run:
java -cp . com.david.Greet
The "com" directory should not be current, it should be a child directory to current. You need step one level upper and launch again. No extra care about classpath should be needed at this point.

Categories

Resources