I have 3 files:
/a/A.java
/a/aa/AA.java
/b/B.java
and B.java depends on A.java and AA.java.
I basically want javac -classpath /a /b/B.java to work (i.e. have javac search below /a).
Is there any way I can do this?
The short answer is no, that's not how classpath directories work.
Each classpath directory is regarded as the root of a package structure. Each package is a directory within the root. So, javac will do so automatically if aa is a package directory and a is the root. You're classes would look like this:
/a/A.java
class A {}
/a/aa/AA.java
package aa;
class AA {}
/b/B.java
package b;
import aa.AA;
class B {
private AA aaInstance;
private A aInstance;
}
Because A has no package, it's placed in the root package.
Otherwise, you have to set each source dir explicitly.
Related
Suppose i have a directory "Animal" (without quotes) and i have java classes in it.How to add that directory to class path in Linux ? I mean suppose i want to use class files in "Animal" directory from some other folder , then i need to add "Animal" to class path .How to do it ?
Also suppose Animal directory has several sub directories and each of the sub directories also have directory having java class files .Then how to add each of them to java package ?
edit : Suppose i have two folders in Ubuntu say Downloads and Documents .Now in Downloads i created a folder called "animal" having a class named Dog.java . Now in Documents folder i created main method in which i try to make an instance of "Dog" class .I want to do it by importing the package "animal" in Download folder. How to do it ?We know that if we want to use some pre-built package in java then we import that package to use classes inside that package .Similarly suppose i want to import my own package , what i need to do ? When i write import and compile then it says package "animal" does not exist .So what i do so that it doesn't gives compilation error .Can some one elaborate by giving an answer .
I did lot of work before asking this question and when i was unsuccessful i finally asked this question .
To work this out in detail, with some code- the folder structure is:
~ (user folder)
~/Documents
~/Documents/useanimal
~/Documents/useanimal/UseDog.java
~/Downloads
~/Downloads/animal
~/Downloads/animal/Dog.java
The code for the two classes is below. To compile:
cd ~/Downloads
javac animal/Dog.java
cd ~/Documents
javac -cp ~/Downloads useanimal/UseDog.java
To run:
cd ~
java -cp Documents:Downloads useanimal.UseDog
and it will output
UseDog:main
Dog created
Code:
animal/Dog.java
package animal;
public class Dog {
public Dog() {
System.out.println("Dog created");
}
}
useanimal/UseDog.java
package useanimal;
import animal.Dog;
public class UseDog {
public static void main(String[] args) {
System.out.println("UseDog:main");
Dog dog = new Dog();
}
}
I have a class called MyClass in the file MyClass.java file (code mentioned below)
package myclass;
class MyClass {
public int add (int a, int b){
return a+b;
}
public static void main(String args[]) {
MyClass obj = new MyClass();
System.out.println(oobj.add(2, 3));
}
}
I am compiling the class with
javac MyClass.java
But I am trying to run the class using
java MyClass
or
java myclass.MyClass
I am getting the Error
Error: Could not find or load main class MyClass
But, I am able to run this program if I omit out the package name.
where am I going wrong?
Make sure that you are inside the parent directory of the package folder (the folder in which your compiled class file is), and execute the following command:
java myclass.MyClass
Below is an example file structure:
bin
-> myclass
-> MyClass.class
In the example structure above, you would have to execute the command from the "bin" directory.
Also, define the class as public and recompile the java source file.
I ran into this too. It's very frustrating for someone from other languages. The key here is, the java file has to be in the right directory depending on the package declaration.
if the java file Test1.java starts with
package com.xyz.tests;
Then the java file Test1.java needs to be in directory com/xyz/tests
You can compile and run as
javac com/xyz/tests/Test1.java
java com/xyz/tests/Test1
Good luck.
You Need To Compile The Class using :
javac -d ./myclass
I get my example to run by
java <package>.<class>
From parent directory of package
Given the following classes:
package a.b;
public class C {
public B b;
}
and, in a different file:
public class B {}
Standing in folder folder, executing
javac B.java
javac a/b/C.java -cp .
will generate B.class inside folder and will return a Symbol not found on the second command.
How (without making any modification to any of the classes B and C) can I compile C.java?
EDIT: maybe this can't be done
If you are creating both the classes in same java file then only one class has to be as declared "public" class. And the file name should be given as
public class name.java
Also your way of creating an instance of class B is incorrect. It has to be done as follows:
B b = new B( );
Make sure to compile as:
javac -d . C.java
and make sure to remove the public access modifier from class B.
Why compile them separately?
javac B.java a/b/C.java
I believe this will work fine, since javac will be able to see both of your files.
You won't even need the -cp option, since you're already telling javac about everything you need.
I'm having a strange error. I have 2 classes in the same package but they can't find each other. From what I remember, as long as the classes are in the same package, they should be able to call each other's methods.
My code looks similar to this:
in A.java:
package com.mypackage;
public class A{
public static int read(){
//some code
}
}
in B.java:
package com.mypackage;
public class B{
public static void main(String args[]){
int x = A.read();
}
}
and it's giving me a cannot find symbol variable A error.
Both of these classes depend on some .jar files, but I've already included the path of those jars to CLASSPATH and A.java compiled fine, but B can't find A for some reasons...
When I remove the package com.mypackage; in both classes then they compile fine.
Since you're compiling Java files that are in distinct packages, you'll have to ensure that they compile to the appropriate directories.
You can use this invocation to do just that. Substitute $SRC with the location of your source files, and you can let $BIN be the current directory, or some other location on your machine.
javac -sourcepath $SRC -d $BIN A.java B.java
When you want to run them, you have to add them manually to the classpath again (but that's not such a bad thing).
java -cp $BIN com.mypackage.B
This invocation should work; just made sure of it with A.java and B.java residing on my desktop. With the -d flag, that ensured that when they compiled, they went to the appropriate package folder scheme.
It should be:
A.java
package com.mypackage;
class A {
public static int read(){
//some code
}
}
B.java
package com.mypackage;
class B {
public static void main(String args[]){
int x = A.read();
}
}
i have two programs
one in directory /home/redhat/Documents/java1/j1
Demo1.java
package j1;
public class Demo1
{
public void print()
{
System.out.println("hi");
}
}
and the other in directory /home/redhat/Documents/java1/j
Demo2.java
import j1.*;
public class Demo2
{
Demo2()
{
Demo1 d=new Demo1();
}
}
when i say
javac -classpath /home/redhat/Documents/java1/j1 Demo2.java
i get the following error
Demo2.java:2: package j1 does not exist
import j1.*;
^
Demo2.java:7: cannot access Demo1
bad class file: /home/redhat/Documents/java1/j1/Demo1.java
file does not contain class Demo1
Please remove or make sure it appears in the correct subdirectory of the classpath.
Demo1 d=new Demo1();
^
2 errors
i want to access instance of Demo1 in Demo2
please help.
Your classpath is wrong. You should point to the root of any declared packages:
javac -classpath /home/redhat/Documents/java1 Demo2.java
Other previous step that I miss is the compilation of Demo1 class. Javac compiler will look for ".class" files, not ".java" ones. So before executing that you need:
javac Demo1.java
As an improvement I would suggest you that you declare your second class inside package "j" instead of default package, since it is not a good idea to have root source paths inside another root path that already contains packages.
The classpath option specified in the command line of the javac executable is used to define the user classpath location where the compiler may find the compiled class files of types. In other words, the compiler expects compiled .class files in the user classpath.
In your case, you have a source class file, in which case you should use the sourcepath option of javac:
javac -sourcepath /home/redhat/Documents/java1 Demo2.java
javac will locate the j1 package under the user class path and hence resolve the type.