We are working on java classes in order to customize it. Before this customization, we just want to check compile/decompile process of existing java jar file (MMC.jar).
We collected all java class file under MMC.jar using jd-gui tool
So when we were compiling the existing MMC.jar (without customization) it is giving attached 6 errors
./com/mmc/model/acknowledgement/package-info.java:7: error: illegal start of
type abstract interface package-info {}
^
./com/mmc/model/acknowledgement/package-info.java:7: error: = expected
abstract interface package-info {}
^
./com/mmc/model/customer/package-info.java:7: error: <identifier> expected
abstract interface package-info {}
^
./com/mmc/model/customer/package-info.java:7: error: illegal start of type
abstract interface package-info {}
^
./com/mmc/model/customer/package-info.java:7: error: = expected
abstract interface package-info {}
^
6 errors
This is the content of package-info java file
package com.mmc.model.customer;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
#XmlSchema(namespace="http://www.iflex.com/mmc/model/customer",
elementFormDefault=XmlNsForm.QUALIFIED)
abstract interface package-info {}
Regards
Ali
The reason you've got compilation error is package-info is not allowed identifier. You cannot name objects like classes, interfaces, variables, etc, with - character. See this answer for more detail Naming rules.
But the problem is that you use package info file in wrong way. The aim of package-info.java file is to add brief description of package in javadoc: what the purpose of this package, responsibilities and contents. You shouldn't declare any classes or interface here. All you need to write in this file is package name in which this file located and package summary javadoc. A #sidgate comment refers to a good answer about package info file responsibility.
Related
I am trying to recompile a JAVA8 code in JAVA11.
Getting below compilation errors.
error: reference to Module is ambiguous
private Module module;
both interface com.module.Module in com.module and class java.lang.Module in java.lang match
Being new to the Java not able to fully understand the root cause. Any information will be of great help.
both interface com.module.Module in com.module and class
java.lang.Module in java.lang match
The error is mostly because of the new class java.lang.Module introduced in Java-9.
Just use the fully qualified name while referencing the interface/class that you've defined as:
private com.module.Module module;
Alternatively, as pointed out by Alan and Holger in comments and from the release notes of Java-9, you can explicitly specify the import for your Module class as :
import com.module.Module;
I have written an annotation processor that generates a builder class for my classes annotated with #DataBuilder
#Target(AnnotationTarget.CLASS)
#Retention(AnnotationRetention.SOURCE)
annotation class DataBuilder
My classes annotated with this annotation are located in the com.my.package.model package and the generated builder class is also located in the same package com.my.package.model but in the generated directory of course build/generated/source/kapt/debug/com/my/package/model/MyModelBuilder.kt, I can use these generated classes fine inside of my model classes(written in Kotlin)
BUT I can NOT use the generated MyModelBuilder Kotlin class inside of a java class as a class member
package com.my.package.home;
import com.my.package.model.MyModelBuilder;
public class Home {
MyModelBuilder builder; // <=== AS recognizes the class, but I'm having an compilation issue
}
Android Studio recognizes the class, but I’m having this compilation issue
com/my/package/home/Home.java:4: error: cannot find symbol
MyModelBuilder builder;
^
symbol: class MyModelBuilder
location: class Home
it’s weird because I can use this generated builder class only inside of methods, this code compiles fine:
package com.my.package.home;
import com.my.package.model.MyModelBuilder;
public class Home {
public void hello() {
MyModelBuilder builder;
}
}
could somebody here help me to understand this behavior and how to fix this? In advance, thanks!
UPDATE
I just created this repo with the necessary code to replicate the issue
https://github.com/epool/HelloKapt
The project works fine after cloning and running, to replicate the issue please un-comment this line https://github.com/epool/HelloKapt/blob/master/app/src/main/java/com/nearsoft/hellokapt/app/MainActivity.java#L13
Note: If I convert my MainActivity.java class to Kotlin(MainActivity.kt) the issues is NOT reproducible and works fine, but I don’t want to do so due to some project limitations so far
Kotlin Issue: https://youtrack.jetbrains.net/issue/KT-24591
Looking at your Github project, I notice that you don't declare a dependency on kotlin-stdlib-jdk7 in the app module. When I build the module, compiler emits the following warnings:
warning: unknown enum constant AnnotationTarget.CLASS
reason: class file for kotlin.annotation.AnnotationTarget not found
warning: unknown enum constant AnnotationRetention.SOURCE
reason: class file for kotlin.annotation.AnnotationRetention not found
warning: unknown enum constant AnnotationTarget.CLASS
reason: class file for kotlin.annotation.AnnotationTarget not found
Since kotlin-stdlib-jdk7 is declared as implementation in the annotations module, the app module doesn't see it as a transitive dependency, that might be the reason why compilation fails. To fix it, you should probably declare the correct dependency in the app module, or at least use apiElements scope for kotlin-stdlib-jdk7 in annotations.
The fact that the IDE doesn't notify you that compilation failed might be a tools bug, but there's definitely no underlying Kotlin compiler issue.
Can we create more than one package in a single .java file? Like:
package parent;
package parent.child; // parent exists already from the previous command
package dada.papa.beta; // dada & papa does not exits but -> for creating beta java will automatically create dada & papa
public class b{
/* see the ReadMe.txt file
here i am just creatign packages;
*/
public static void main(String[] args) {}
}
I just wanted to practice making lots of packages and sub packages in a single go.
Error:
Unix-Box ~/making_sub_packages$ javac -d . b.java
b.java:2: error: class, interface, or enum expected
package parent.child;
^
b.java:4: error: class, interface, or enum expected
package dada.papa.beta;
^
2 errors
No, you can't do that.
The package statement must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. You can read more here:
https://docs.oracle.com/javase/tutorial/java/package/createpkgs.html
I've got structure of catalogs:
/home/etc./studies/JAVA/pack/Print.java
/home/etc./studies/JAVA/Lab2/zad1/pkg/A.java
/home/etc./studies/JAVA/Lab2/zad1/B.java
A is a class in package "pkg"
Print is a class in package "pack"
B imports the packages "pkg" and "pack".
When I tried to compile B.java, I get an error:
B.java:4: error: cannot access A
public class B extends A{
^
bad class file: /home/etc./studies/java/A.class
class file contains wrong class: pkg.A
Please remove or make sure it appears in the correct subdirectory of
the classpath.
Is it possible to include that packages without reorganization structure of files ?
It's clearly saying that:
class file contains wrong class name: pkg.A
that means probably you declared class name as pkg. An instead of A.
if you declare the package names as correct like this
home/etc/studies/JAVA/Lab2/zad1
home/etc/studies/JAVA/Lab2/zad1/pkg,
/home/etc/studies/JAVA/Lab2/zad1/B.java
You won't get the compilation error.
package home.etc.studies.JAVA.Lab2.zad1;
import home.etc.studies.JAVA.Lab2.zad1.pkg.A;
public class B extends A {
'enter code here`
}
You cannotreference Classes in a default package. Put every class into a package.
My directory structure is as follows:
/WorkingDirectory
MyCollection.java
/au/edu/au
/UserInterface
UserInterface.java
/Collection
Album.java
CDAlbum.java
DVDAlbum.java
CollectionFactory.java
Where Album.java is an interface implemented by CDAlbum.java and DVDAlbum.java. Each .java file has the appropriate
package au.edu.uow.UserInterface;
or
package au.edu.uow.Collection;
line.
In UserInterface.java I declare an ArrayList of type Album, which gives me compile errors of
.\au\edu\uow\UserInterface\UserInterface.java:9: error: cannot find symbol
private ArrayList<Album> myCollection;
^
symbol: class Album
location: class UserInterface
I am compiling from MyCollection.java, which has import statements for both packages.
what is causing this error? I tried adding
import au.edu.uow.Collection
to UserInterface.java, but to no avail.
You need either:
import au.edu.uow.Collection.*;
or:
import au.edu.uow.Collection.Album;
Using wildcard imports is more convenient but some consider this a bad practice. This is a matter of style; decide for yourself.
It is also considered good style for package names to be in all-lower-case, although this is not enforced by the compiler.
So, you folder structure looks like /au/edu/au & your trying to import from au.edu.uow
Apart from what JimN has suggested, I think you'll find that Java will complain that te package doesn't exist or the files in /au/edu/au are in the wrong package
Either change the directory structure or package structure to match each other