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.
Related
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
I have a package called "test" and in that have a public class that contains the main method in a file called ABC.java.
package test;
public class ABC{
public static void main(String[] args) {
new T1();
}
}
In that same package "test" I have two default classes T1 and T2 in a file called T.java
package test;
class T1 {}
class T2 {}
when i try to compile it it says cannot find symbol new T1(). When I put T1 in a separate file T1.java then it compiles fine. Why java is unable to find package private class in the same package.
javac will automatically compile all the linked file used in the file you are compiling if there .class files are not found. Like in your case ABC.java. But one thing to notice is javac will not search for all the files with .java extension to be compiled. But it will look for the file name with the same name as the class. Like in your case T1.
So if you will compile T.java and then compile ABC.java it will run as expected. But if you compile ABC.java and not T.java compiler will not find T1.class then it will look for T1.java, but it will not found it too, which will give you an error. On the other hand if you will rename T.java to T1.java it will work as expected.
In Java, When a class file name(T1.java) is same as class's name(T1) without public keyword, this class is public under this package(test)'s class.
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 have written the following code:
package abc.def;
public class test {
public void test() {}
public void disp() {
System.out.println("in disp");
}
}
then I used following command to compile:
javac -d . test.java
it works fine, but when I tried to import the class "test" using "import abc.def.*" it does not import test class, the code is :
import abc.def.*;
public class checktest {
public static void main(String a[]) {
test t = new test();
}
}
following error is generated:
D:\javaprograms>javac checktest.java
checktest.java:8: cannot access test
bad class file: .\test.java
file does not contain class test
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
test t = new test();
^
1 error
I also had the same problem.
No additional classpath is required to set.
According to your scenario, your working directory might contains test.java file. You can just remove the test.java file from the working directory and compile using javac checktest.java.
It will work.
Thanks.
Britto
Did you make the proper directory structure? You need to have the test.java file in abc/def if that's the package name you want.
You can also point to the compiled test.class file with -cp flag
Example:
javac -cp test checktest
Your directory structure should look like this:
current working directory
checktest.java
abc
def
test.java
Then, from the directory on the top, you can compile checktest:
javac checktest.java
This will automatically find (and compile) test.java too. If you only want to compile test, do it this way:
javac abc/def/test.java
Then all the class files will be in the right directories, too.
It seems that you have by mistake compiled test.java in the topmost directory itself, therefore the JVM is picking test.class from the top most directory and also from abc\def\test.class hence conflict is happening.
please type: ls test* in the top most directory and confirm if that is the case and delete this extra test.class and then recompile.
first know this - To use the package in other programs, compile the .java files as usual and then move the resulting .class files into the appropriate subdirectory of one of the directories referenced in your CLASSPATH environment variable.
For instance if /home/name/classes is in your CLASSPATH and your package is called package1, then you would make a directory called package1 in /home/name/classes and then put all the .class files in the package in /home/name/classes/package1.
Now suppose your classpath is /home/name/classes then compile
package abc.def;
public class test {
public void test() {} public void disp() { System.out.println("in disp"); }
}
using $ javac -d /home/name/classes test.java
Now put this code
import abc.def.*;
public class checktest {
public static void main(String a[]) {
test t = new test();
}
}
inside the folder
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.