How to use a single .class file in a java package? - java

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.

Related

Do the java packages differ from the concept of directory or folder? if yes how?

I have a created a class by writing code as:
pacakge packtst;
public class Class1
{
public static void main(String args[])
{
System.out.print("This is class1");
}
}
Then i compiled the program by
javac -d . Class1.java
It created a subdirectory in my current directory, named packtst. Then I went into the packtst deirectory. Within it i made another class as:
public class Class2
{
public static void main(String args[])
{
System.out.print("this is class2");
}
}
And compiled it in the packtst directory by
javac Class2.java
So, now in the package packtst i have two class file Class1.class and Class2.class
Now I try to run Class1 from outside of the directory by
java packtst.Class1
It ran well. But then I tried to run Class2 from the same directory:
java packtst.Class2
It said couldn't find or load main class packtst.Class2
Why?
I have a created a class by writing code as:
pacakge packtst;
public class Class1
Then i compiled the program by
javac -d . Class1.java
That was your first two mistakes. The .java file should have been in the packtst directory, relative to where you started, and you should have compiled it via:
javac packtst/Class1.java
Then I went into the packtst deirectory.
That was your second mistake. You should have stayed where you were.
Within it i made another class as:
public class Class2
That was your third mistake. As Class2 doesn't have a package statement, it should be in the directory that you started in.
And compiled it in the packtst directory by
javac Class2.java
That was your fourth mistake. The javac command line is correct but it should have been issued from the directory you started in, i.e. the one that contains the packtst directory.
So, now in the package packtst i have two class file Class1.class and Class2.class
That was your fifth mistake. Your directories from where you started should now contain:
packtest/Class1.class
Class2.class
Now I try to run Class1 from outside of the directory by
java packtst.Class1
It ran well.
So you must have now been in the directory you started in.
But then I tried to run Class2 from the same directory:
java packtst.Class2
That was your sixth mistake. Class2 isn't in the packtst package, because its source code doesn't contain such a package statement. The command should have been:
java Class2
It said couldn't find or load main class packtst.Class2
Correct.
Why?
Because there is no such class. The fully qualified name of Class2 including all its declared packages, is Class2.
In java, it is not enforced to have the same directory structure as your package structure referenced in that class. Since you are using the '-d' option to compile your code, the compiler will create a directory structure similar to the package structure to put the compiled class.
In the case of Class2, since you did not define any package reference for it, "packtst.Class2" will not exist.
Directory structure is a best-practice (to reflect the package structure in the source directory structure) and not something enforced by java compiler.
Also, package structure is the structure in which all your compiled classes would be present in the jar (or your complete project)
It said couldn't find or load main class packtst.Class2
because Class2 does not have a / belong to a package name packtst
Even the compiled class file is located in packtst the full qualified name of the class is Class2 (the full qualified name of Class1 is packtst.Class1). The package name is part of the (source) and the compiled class file.
That the package structure and the filesystem structure match is only a convention the java jvm and the java compilers use.
If the classes are stored on windows or *nix filesystems there is the convention that you must store a Java class in a directory with a relative directory path that matches the package name for that class.
Java Packages are an abstract construct. They help to organise the Applications into a set of packages. Each package has it's own set of names for the types (Classes, Interfaces,...) in the package, which helps to prevent naming conflicts.
The JLS (Java Language specification) only says about the package structure that it is hierarchical but also that you don't have to store java classes on an hierarchical filesystem you can also store them in a database.
JLS Java 8:
The naming structure for packages is hierarchical (§7.1). The members
of a package are class and interface types (§7.6), which are declared
in compilation units of the package, and subpackages, which may
contain compilation units and subpackages of their own.
A package can be stored in a file system or in a database (§7.2).
Packages that are stored in a file system may have certain constraints
on the organization of their compilation units to allow a simple
implementation to find classes easily.
If the classes are stored in a (hierarchical) file system which is similar to what you find on most PC and UNIX® systems, then the package name relates to the directory structure in which the class resides. This is how the actual java compilers, jvms / class loaders on Windows and *NIX work.
There is a article on the IBM Knowledge Base (Java classes, packages, and directories) which describes the relationship.

How does Javac work for multiple files, directories, classes and source?

I'm trying to figure out how javac works with regard to stuff like sourcepath, classpath and prebuilt classes etc. I'm trying to read the documentation, but can't really make sense of it.
I've tried to think of some sample cases below.
If I'm compiling a single file onlyfile.java which has no dependencies, which has 2 classes A and B , and class A uses class B , does class B need to be defined/declared before A ? Or is javac smart and does multiple passes or something like that ?
root.java uses another class in a file file2.java located in the same folder. If I execute javac root.java , how does javac know to search the folder for the class file and if not found , for source file instead ?
How does the above work if the file2 is located in a subdirectory ?
EDIT:
I read somewhere that import is just a way to cut down on typing rather than "loading" anything like in python.
Suppose that I'm building only 1 java file which uses multiple other classes, and that these class files already exist. Without import, the a.b.c.d part of the class object already tells me where to search for the class file, then why a cp option ?
1) If you compile class A which uses class B then class B will be compelled as well. If you compile class B (which is used inside A, but A is not used inside B), class A will not be compelled. Find more details end examples here.
2) javac searches inside source-path and class-path. If you run javac without arguments like javac A.java it sets classpath and sourcepath to current directory. If requested class is not found neither in classpath nor in sourcepath you'll have compilation error.
3) Java has strict rules for project structure. You can't simply place source file to another folder without updating file content.
Every folder in the project should have folder hierarchy with respect of package declaration.
Definition: A package is a grouping of related types providing access protection and name space management.
for instance if you have class A.java with package declaration like this
package com.mycompany;
The corresponding folder structure should look like this:
com/mycompany/A.java
If you follow this rules compiler will be able to resolve dependencies just like I explained in #1. Find more information here.
For first two options try with javac *.java
Duplicate of Compiling Multiple Classes (Console) in Java

Extended classes compile perfectly in Eclipse but not in cmd

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

Compiling Java Classes Together

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).

how to create a package

I just know how to create a package just by declaring it at the top of the class but I dont know the code for creating it
package myPackage;
class test{
system.out.println("Hello every one");
}
please provide me the code to compile it
This should be your lesson of the day :)
http://download.oracle.com/javase/tutorial/java/package/packages.html
Read from end to end, and don't forget this page http://download.oracle.com/javase/tutorial/java/package/managingfiles.html
Read these for compiling options:
http://www.cs.princeton.edu/courses/archive/fall97/cs461/jdkdocs/tooldocs/win32/javac.html
http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html
A package is 'created' by the folder hierarchy it is in. For example, your test class is in the myPackage package, which has to be in a folder called myPackage.
If your classes are in packages, then Java expects your source files to be in a directory structure that looks like the package structure. In this case, make a directory called myPackage and put test.java in that directory. Then from the directory the myPackage directory is in, run javac myPackage/test.java. This will create a test.class file in the myPackage directory. If test.java had a main function defined in it, you could then, from the same directory, run java myProject.test.
The 'test' class is supposed to be public, and put it under folder "myPackage". The folder is package.

Categories