javac : Compiling a .java file which uses other classes in it - java

HI i have 3 java files
a.java
b.java
c.java
I managed to generate .class files for both a and b using
javac example/a.java
javac example/b.java
but when i do the same for c.java I get the error
error: cannot find symbol b and c
Any suggestions on how i could solve this problem ?
All the java files are in the same folder

You have to have classes a and b in your classpath when you try to compile class c. This allows the compiler to verify that they exist, figure out what methods they have, etc.
javac is pretty sensitive to package names and classpaths. The easiest thing to do is to compile all three at the same time like so javac example/a.java example/b.java example/c.java.
If you go to the parent directory of example (let's call it src), then you can run the following:
javac -cp src src/example/c.java
The reason you have to do it this way is because your classes have their packages listed as example. Because of your package name, javac is looking for the example directory in its classpath, where it expects to find a.class and b.class.

Presumably you're not in the example/ directory when you run javac. Try
javac -cp example c.java
Or just cd into that directory. The classpath is not automatically resolved for the classes c.java depends on.

Related

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.

How can I include packages(folders) in the compilation of a java project through terminal?

So, I want to compile a java benchmark.
I am working inside the folder /home/username/Tools/myTool/folder2.
I am compiling with javac -cp /home/username/Tools/appv1.0/ *.java
folder1 compile sucesfully because it did not have any dependencies on packages.
Right inside folder 2, that I have the problem, there are 5 folders residing as packages(having some java classes), but the compiler can not recognize them, so I need to put that explicitly.
And I keep getting errors like this
JClass1.java:22: error: package crypt does not exist
import crypt.*;
^
JClass2.java:23: error: package series does not exist
import series.*;
^
So, how can I direct the compiler towards those packages?
Thanks in advance.
You must write the command to compile as:
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/YOUR_FILE_NAME.java
As a result bin/com/example/Application.class file should be created. If Application.java uses other classes from the project, they all should be automatically compiled and put into corresponding folders.
For more info.
The anwser provided by yogx was the right one after all!!
Problem solved.
javac -cp dir1/*:dir2/* MainClass.java
Thank you all!

How do I compile a Java class which uses another Java class as an Object?

I have an A.java class which uses B.java class as an object.
When I compile A.java class, it throws a compile error message, since the Java compiler can not reference the B.java object at all. So, here is my question:
How do I compile A.java class if it includes another B.java class?
Eclipse is a great tool, but this tool is not useful when I need to compile a Java file for Java beans.
You haven't explained how you're trying to compile A.java, or whether you've already compiled B.java. If you haven't compiled either of them yet, just compile them both together, e.g.
javac -d bin path/to/A.java path/to/B.java
If you've already compiled B, you need to make sure you've got the classpath right, e.g.
javac -d bin -cp path/to/Broot path/to/A.java
Note that the classpath value shouldn't be the B.class file itself, nor even the directory containing B.class - but the root of the output hierarchy. So if B is in package foo.bar, and B.class is in directory /x/y/z/foo/bar you'd write:
javac -d bin -cp /x/y/z path/to/A.java
make sure that the dependency(jar file) and the current directory is in the classpath when you compile a class.
This is a duplicate of the question: Can I use JAVAC to compile a project with multiple files and directories?
Depending on the type of object B you must add it to the classpath. If it's in a jar file (library) you should the -c option on command line. For adding other source files use the -s option.
Once you start bigger projects it will be hard to keep doing this manually on the command line. Look at a proper IDE like eclipse. It does all that for you. You only have to set the classpath in the properties once and then you can just compile with the click of some menu options (or set automatic building or use shortcut keys).
The error seems to be that the class B is not yet compiled. Could you check class B for errors, compile B, and then go for A?

Do not understand following use of -classpath by javac

I am not very clear with the following question from SCJP Book (I read the solution and explanation though) ..
Consider the following directory structure :-
foo --> test --> xcom --> A.class, B.java
Here foo, test and xcom are directories. A.class and B.java are the files in xcom directory.
Following are the source codes of corresponding files:-
A.java
package xcom;
public class A { }
B.java
package xcom;
public class B extends A { }
The default classpath is /foo.
Now, in order to compile B.java, I keep my current directory as test and give :-
javac -classpath xcom xcom/B.java
Here I give the classpath as xcom which has A.class. But still it does not find class A. Why is it so??
If your classes are in package xcom, then your classpath needs to be at the directory directly above that. In this case, the classpath should be foo/test.
And if your current directory is foo/test, then this should be your javac:
javac -classpath . xcom/B.java
Because you have to specify classpath root to -classpath argument, like javac -classpath . xcom/B.java. To compile class B java compiler requires class A, it tries to locate class A file in {classpathroot}/xcom/.
Note: . - is a current directory
I think the root cause here is a misunderstanding of a "fully-qualified name" in Java.
The fully-qualified names of your two classes are xcom.A and xcom.B. Their source is in files A.java and B.java in a directory named xcom; the fully-qualified names dictate the directory structure. When you are going to use the files, either to compile them or run them, the classpath contains one or more locations from which the fully-qualified names can be found; so java is looking for xcom\A.java and xcom\B.java (when compiling) and xcom\A.class and xcom\B.class (when running).
That is why the classpath needs to specify the directory that contains xcom.
As you progress to more complex environments: the classpath can be a list of such locations; each location is separated by a semicolon on windows and a colon on unix systems. Each location can be a directory, as you've already seen, but it can also be a jar file. jar files are in zip file format, and zip files have a directory structure just like disks do. So you could zip up your class files, maintaining their xcom parent (but not their full paths), and specify the jar file in the classpath instead of a directory.
I know the question was already answered somewhat, but thought you might like the background explanation as well.

Java: class resolved?

I hope this question is not repeated. But just can't find answer anywhere:
I have ONE folder containing two files one A.java another B.class.
Now in A.java I am trying to declare
public class A extends Applet{
...
B aB;
}
The compiler gives me:
B cannot be resolved to a type
I read a lot of posts that say if the files are in the same folder, I don't need to import. Could anyone help me to "resolve" this problem?
Thanks much appreciated!
-----------SOLVED! - SEE ANSWER BELOW------------------
The .class files need to reside in a directory referenced by the classpath variable. Usually you put your .java files in one directory (src), compile to another directory (bin) and have external .class files in a third directory (lib). The commands will look like this:
# compile
javac -sourcepath src -classpath lib -d bin
# run
java -classpath bin:lib A
Using an IDE like eclipse should help a lot here as it takes care of most of the details
The simple case that you've posted works for me. I'd check the following things:
Are you sure that B.class is present in the same folder as A.java?
Are you running javac from that folder?
Have you typed the class name B correctly everywhere in your program? This includes capitalization, as Java identifiers are case sensitive.
Are there any package declarations in your program? If there are, none of this is going to work, since you're implicitly using the default package by just throwing everything into a folder.
The compiler looks for *.class file in its class path. It will only look for *.java files in the same source directories. You need to set the class path to include the directory.
Or you could use an IDE which sets all this up for you and saves a lot time in the process.

Categories

Resources