I thought I new this, but when I create an object out of an class in a seperate file, it only compiles if that class is defined in the same directory, or if I import it from the library. I assumed it used classpath to search for the included class files, but when I add a random directory to classpath(and only place that file there), it still complains that the class is not defined and won't compile. Where does it know to look for classes at compile time?
Example
public class SomeClass {
public SomeOtherClass SoC; // If this class is not in library or same directory -- won't compile.
}
// If this class is not in library or same directory -- won't compile
Correct. Your classes need to be in the classpath, or in a .jar that you specify.
When compiling you would use
javac -classpath .:/some/other:/another:/some/foo.jar
You then import whatever you need in your .java files (your code)
Besides the import you also have to have folders on the classpath that represent the class' package.
Example:
Class com.whatever.SomeClass is located in src/java/com/whatever/SomeClass.class.
Now, the classpath should contain src/java/ and from there the package com.whatever is looked up.
If the classes are in a .jar file, you'd put the jar in the classpath. Inside the .jar you'd again have com/whatever/SomeClass.class (note that .jar is basically a zip like format).
Related
I want to write a code like this
package mypackage;
public class A extends B {
}
But all that I have is the B.class file which is compiled from a single B.java file with no package specified.
Could anyone help me out?
Thanks!
I've tried putting B.class in my ./src and A.java in ./src/mypackage and run javac -cp src ./src/package/A.java but it wouldn't compile. It wouldn't compile neither if I put B.class in the same folder as A.java
You can't. By your description, class B is in the default (unnamed) package. The Java language rules do not allow classes outside the default package to refer to classes in the default package.
The only way you can use this class is to put A in the default package as well. That is, move it one directory up, and remove the package statement.
Alternatively, you need to have the sources of class B (or decompile it), move it into a package and add a package statement, and recompile.
I have some classes that are located in classes/com/scja/exam/tutorial/planets on the filesystem. I'm trying to compile a file that is located in classes/com/scja/exam/tutorial/. Do I have to manually import this ? I'm trying to compile using this command:
javac -d classes -cp classes/com/scja/exam/tutorial/planets/:.
src/com/scjaexam/tutorial/GreetingsUniverse.java
It seems like java cannot find the classes this file needs.
Understand that when Java searches for a class named aaa.bbb.ccc.MyClass, it searches each directory in the classpath for a directory named "aaa". Finding one, it will search that directory for "bbb", then, if that's found "ccc", then actually look for "MyClass.class". If you make your classpath -cp aaa/bbb/ccc then Java will look there, find no "aaa", and give up.
A class in a package must import classes it uses (without using their fully qualified name) that are not in the same package (an not in java.lang). The directories where the classes are stored must match the packages, but you could have several root directories (or jars) containing the classes.
Your command doesn't work because you put the directory of a package (classes/com/scja/exam/tutorial/planets/) in the classpath, instead of putting the root directory (classes).
I am a newbie in java I Want to know that what is the default directory for packages in java ? I Mean if i compile a java file which contains a package statement,and i compile it without using -d option in javac command,then where will be the package created ? eg.
package test;
class Test{}
and compile it using javac Test.java
then where will be the package created?
Thanks
If you don't specify -d, the class file will be created in the same directory as the source file.
That's fine if you're already storing your source in a directory structure matching your package structure (and if you're happy for your source and class files to live in the same place) but if your source structure doesn't match your package structure, you'll basically end up with class files in locations where they can't sensibly be used.
Personally for anything other than quick throwaway (usually Stack Overflow :) code I would make the following suggestions:
Avoid using the default package
Keep your source code in a directory structure (e.g. with a root of src) matching package structure
Generate class files into a separate directory structure (e.g. with a root of bin or out or classes)
(Sorry, misread the question to start with.)
-d option in javac command is use to specify where to generate the class file,if you don't specify it,then the .class file will be created in the same directory where your .java file is present.
There is no directory created when you do not specify the package!
all the .class files will be created directly in the output folder
public class A{}
if you compile this to output folder ,
output/a.class is created
My program consists of classes that extend other classes. The problem is, when I try to compile them in cmd, system says "Cannot find symbol". Some sites discussing similar problems mention including the source folder in the CLASSPATH. I did as they said and it still gives the same error. However, it runs smoothly in Eclipse. Any ideas why?
Make sure that you compile the top level class first. If you try to compile a class extending other class, and you don't have compiled .class file for that class, you will get that error.
package pkg1;
class A { }
package pkg2;
import pkg1.A;
class B extends A { }
For the above code, you should compile the .java file containing your class A first to get your .java file containing class B compiled. Also, set the path containing your class files in your classpath.
In Eclipse, it works because it automatically compiles your classes as you save them.
NOTE: -
If you are having your classes under some packages, then compile your java files using this command: - javac -d . A.java. This will create a folder for the package name and put the class file in that folder automatically. Then your class B would be able to find it
I am using Notepad++ to write my Java code and Command Prompt to compile and run it.
Following is my sample Java code,
package abraKadabra;
public class SuperClass{
protected int anInstance;
public static void main(String [] abc){
System.out.println("Hello");
}
}
However, this file is in the following folder structure :
"usingprotected\superPkg" (usingProtected is a folder somewhere in the hierarchy in C:)
So, my package name here should be something like usingProtected.superPkg instead of abraKadabra as I wrote it.
But, when I compile this Java code from command prompt, it compiles fine with no error or warnings. Why is it so? Shouldn't the package name adhere to the folder structure?
And if it should, how would it adhere?
For e.g. if my package name is usingProtected.superPkg, will the compiler check in the reverse order. The present working directory should be superPkg, then the parent directory should be usingProtected and its done. Is it how it checks the folder structure with package name?
The Java language specification doesn't force files to be in a certain directory. It optionally allows the compiler to require that public classes are in files with the same name of the class, but I don't think there's anything similar for packages. Section 7.2.1 talks about possible storage options in a file system, but it doesn't say anything about enforcing source code structure, as far as I can see.
However, it's best practice - and a pretty much universally accepted convention - to reflect the package structure in the source directory structure... and javac will use this to try to find source files which aren't explicitly specified to be compiled.
Note that if you're compiling from the command line, by default each class will appear in the same location as the corresponding source file, but if you use the "-d" option (e.g. "-d bin") the compiler will build an appropriate output directory structure for you, rooted in the specified directory.
After experimenting a bit, I got the way how to use package name and run Java class files from command prompt.
Suppose following is my Java source file:-
package mySample;
public abstract class Sample{
public static void main(String... a){
System.out.println("Hello ambiguity");
}
}
This file is in directory "D:\Code N Code\CommandLine".
Now, when compile the source code (by going to the above directory from cmd) using following command:-
javac -d . Sample.java
This automatically creates "mySample" folder in my current directory. So, my class file Sample.class is present in directory "D:\Code N Code\CommandLine\mySample". Compiler created this new folder "mySample" from the package name that I gave in my source code.
So if I had given my package name to be "package com.mySample", compiler would create two directories and place my class file in "D:\Code N Code\CommandLine\com\mySample".
Now, I am still in the present working directory i.e. in "D:\Code N Code\CommandLine". And to run my class file, I give the following command:
java mySample.Sample
So, I give the complete hierarchy of package and then the class name. The Java Interpreter will search the current directory for "mySample" directory and in that for "Sample.class". It gets it right and runs it successfully. :)
Now, when I asked that why it compiles my wrong package source code, it would compile the code successfully though, but it gives NoClassDefFoundError when I run my class file. So above method can be used to use package names from command line.
If you're compiling a single class, javac doesn't need to look elsewhere for it. It'll just compile the file as is and put the resulting .class into the same folder. However, you generally won't be able to use the class til you put it into an "abraKadabra" directory in one of the directories in the class path.
If your class uses another class in the package, though, you might have problems compiling it where it is, for the same reason (javac wants to find the class and make sure it has the methods and such that your class uses).
Java compiler does not check the directory structure when it compiles source files. As you mentioned, suppose you have a source file that starts with the directive
package abraKadabra;
You can compile the file even if it is not contained in a subdirectory .../abraKadabra . The source file will compile without errors if it doesn’t depend on other packages. However, the resulting program will not run (unless also including package name in execution). The virtual machine won’t find the resulting classes when you try to run the program.