Java classpath: Bad class file: contains wrong class - java

I have the following project structure:
MyProject
+-Main.java
+-package1
+-ClassA.java
+-package2
+-ClassB.java
+-ClassC.java
After compiling using javac *.java from the MyProject folder, the .class files are created in the folders where the corresponding .java files are located.
This is an overview of my Main.java file:
import package1.*;
import package1.package2.*;
class Main {
public static void main(String[] args) {
ClassB test = new ClassB();
}
}
And this is the error I get when compiling:
Main.java:15: error: cannot access ClassB
ClassB test = new ClassB();
^
bad class file: ./package1/package2/ClassB.class
class file contains wrong class: package2.ClassB
Please remove or make sure it appears in the correct subdirectory of the classpath.
I have made sure that the package structure matches the folders.
In addition, I tried compiling the project manually by using
javac -cp package1/package2 package1/package2/*.java
javac -cp package1 package1/*.java
javac -cp . Main.java
Which unfortunately produces the same error.
My verdict is that this issue is caused by the location of the .class files in package2, which the compiler can't seem to handle properly but I'm not sure how to modify my project structure.

Related

Unable to run javac in a method importing a class in a jar

I want to compile Entry.java which imports a Utilities JAR.
I'm trying to run javac in the Entry.java to create its class file. But every time I get the package utilities does not exist error.
I tried this command from the Project folder, but it didn't work: javac .\main\Entry.java.
The directory structure is this:
Project
Manifest.txt
main
Entry.java
utilities
Utilities.jar
Entry.java is a simple file importing Utilities (a JAR file):
package main;
import utilities.Utilities;
public class Entry{
public static void main(String[] args){
System.out.println(Utilities.word());
}
}
Utilities.java has only a method that returns a string:
package utilities;
public class Utilities{
public static String word (){
return "Utilities";
}
}
If I understood correctly if I run javac from the top directory (called Project) all the subdirectories will be in the CLASSPATH, so I don't need -cp here. I would need it if I run javac from inside the main directory.
(But I've tried anyway. But javac -cp .\utilities\Utilities.jar .\Entry.java didn't work as well)
So the whether the problem is the import or I didn't actually understand how to work with JAR files.
Or none of the above.

How to compile and run java with package

HI I'm having trouble with my java compille
I made folder named 'Test'. In this folder i make two folders, one is src, another one is bin. Then I made Test. java in that src folder
package Test;
import java.io.*;
public class Test {
public static void main(String args[]) {
System.out.println("hi");
}
}
i saved it and back at Test folder and then i compile like this
javac -d bin src/Test.java
Thus, i have Test folder in bin folder.
finally in Test folder i write this command
java -cp bin/Test Test
unfortunately, it says can't find Test class
How can i run this code???
When your class is in a package, the name of the class includes the package. Thus Test.Test is the Test class in the Test package. -cp bin tells java that the classpath starts in bin.
java -cp bin Test.Test
# classpath main-class
"-cp" expects a directory, not a file. Give it the ./bin/ directory, not the file you're trying to execute.
java -cp bin Test.Test

Creating a directory to store source files and class files separately. However, I'm not able to extend one class to another in the same package

Following is the directory flow which i'm trying to create:
company folder.
under company folder I have class folder and source folder.
under source folder I have pack1 folder.
->company\class
->company\source\pack1
Inside pack1 folder I have save two source files : A.java and B.java where class B extends class A.
->company\source\pack1\A.java
->company\source\pack1\B.java
Now I want to compile class A and class B and store the class files of A and B under Company\class\pack1
The class files should be stored this way:
->company\class\pack1\A.class
->company\class\pack1\B.class
General code:
package pack1;
public class A
{
//class A implementation
}
----------------------------------------
package pack1;
public class B extends A
{
//class B implementation
}
The command i used in command prompt is
F:\company\source\pack1> javac -d ../../class A.java
There was no problem. A.class was created inside company\class\pack1
But, i tried compiling B.java
F:\company\source\pack1> javac -d ../../class B.java
i got the following error:
B.java:3: error: cannot find symbol
public class B extends A
^
symbol: class A
1 error
I searched everywhere. I am unable to sleep because of this. I am a beginner for java and I want to know why I am not able to inherit form class A to class B even though both the source files are inside the same folder.
Please brief me in detail. Thanks
Compiling class B requires that the compiler knows the class A. It is not part of the sources that the compiler is working on and it is not on the classpath. So the 2 solutions I see are:
a) compile the sources together e.g.
javac -d ../../class A.java B.java
b) Make sure that class A can be found on the classpath when compiling B:
javac -d ../../class A.java
javac -d ../../class -cp ../../class B.java

compiling a class with a package throws an error

I'm fairly new to java and am trying to get the basics down. One of them would be dealing with package, I'm trying to implement one class into another using package HelpPack; and upon using javac -d HelpPackage A.java it throws the error javac: file not found: A.java. Thank you for your help
//B.java
package HelpPack;
public class B{
public String name(){
return "Class b";
}
}
//A.java
package HelpPack;
public class A{
public static void main(String[] args){
B b = new B();
System.out.println(b.name());
}
}
Compile all your classes
javac -d . *.java
and run using,
java HelpPack.A
Have a look at javac tool
From the tool doc : By default, the compiler puts each class file in the same directory as
its source file. You can specify a separate destination directory with
-d (see Options, below).
and package tutorial.
-d specifies where the class files will go. Your java files need to be in that directory too and command should be
javac HelpPack/*java
Your package needs to match an actual directory.
So your class A.java should be in the directory HelpPack.
According to documentation :
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of apackage, javac puts the class file in a subdirectory reflecting the
package name, creating directories as needed. For example, if you
specify -d C:\myclasses and the class is called com.mypackage.MyClass,
then the class file is called
C:\myclasses\com\mypackage\MyClass.class.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.

Error java executing: Couldn't find or load the main class

I'm trying to execute a simple java code (I have already compiled it with no problems) but it gives me the next error:
c:\Users\alejandro\Desktop> java HelloWorld.java
Error: Couldn't find or load the main class.
The code is the next:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world!");
}
}
I have set the PATH variable correctly.
I have deleted CLASSPATH variable.
I have both files (.java and .class) in my Desktop.
You're specifying the name of the source file. That's not what you provide to the java command - you specify the class name.
java HelloWorld
This assumes that HelloWorld.class is somewhere on the classpath, which would default to "the current directory".
If you had a package, e.g.
package foo;
public class HelloWorld {
...
}
Then you would want to put HelloWorld.java in a directory called foo, and compile and run from the root directory:
> javac foo\HelloWorld.java
> java foo.HelloWorld
Note how now the fully-qualified class name is foo.HelloWorld, not foo\HelloWorld.
when you run the compiled file, you should use only the class name. The compiled file will have an extension of .class but you should not add any extension. Just use the class name.
change
c:\Users\alejandro\Desktop> java HelloWorld.java
to
c:\Users\alejandro\Desktop> java HelloWorld

Categories