recompile single class in package - java

I am running into this problem when trying to recompile a single class inside a package.
Now this class uses global types and some of these global types reference it. So taking it out of the package really isn't an option.
So when I try to compile it with javac alone, I get invalid symbol errors and netbeans shows it is trying to compile things like classespackage.globaltype. Basically it is searching for the global classes inside of the package. Is there anyway to stop it from doing that?
Here is the code:
Global
public class Global {
example.Main main;
public Global(example.Main m) {
main = m;
}
}
example.Main
package example;
public class Main {
public static void main(String[] args) {
Global g = new Global(new Main()); // COMPILE ERROR
}
}

I get invalid symbol errors
You probably meant "Cannot find symbol" errors? This can be caused by anything. Imported class which is not in the compiletime classpath, methods which does not exist, variables which are out of the scope. You really need to post the compilation errors to get more detailed answers.
At least, this much sounds like that you didn't specify the dependencies (the imported classes) in the compiletime classpath using the -cp or -classpath argument.
Is there anyway to stop it from doing that?
By listening to those errors and taking actions accordingly.
Update as per the posted code example: the cause of the problem is that classes in the default package (i.e. classes without a package declaration) are invisible to classes inside a concrete package (i.e. classes with a package declaration). You need to put Global in a package. Then it's visible (importable) to classes inside a package.

If you reference the global types in a class package, you'll have to include them in your classpath. When you do javac, make sure you include the global types class in your classpath (with the -cp option.)

Related

Kotlin/KAPT Generated Kotlin class is not recognized as class member, but it does inside of methods

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.

Java NoSuchMethodError when Method Exists

I am referencing PlayerUtil.getMovementSpeed(player); in my Speed class, and in my PlayerUtil class, I have the method defined as:
public static double getMovementSpeed(Player player) {
//my code here
}
But whenever the getMovementSpeed method is referenced in my other classes, it throws this error:
java.lang.NoSuchMethodError: net.Swedz.util.PlayerUtil.getMovementSpeed(Lorg/bukkit/entity/Player;)D
I thought it may be that Eclipse was exporting incorrectly, but I rebooted it and tried again with no avail.
EDIT: I did try decompiling the exported jar, and the public static double getMovementSpeed(Player player) method does exist in the exported jar.
EDIT: My friend is also having a similar issue, and is using IntelliJ, so Eclipse is not the issue.
EDIT: Class definition for PlayerUtil:
package net.Swedz.util;
public class PlayerUtil implements Listener {
//getMovementSpeed is defined in here
}
Class definition for Speed:
package net.Swedz.hack.detect.move;
public class Speed implements Hack, Listener {
//my detection methods and method containing PlayerUtil.getMovementSpeed(player);
}
SOLUTION: I found on my own that I had classes conflicting between two plugins on my server. I had one jar with net.Swedz.util.PlayerUtil and another with net.Swedz.util.PlayerUtil both with different contents. I added my project name in all lower case after the net.Swedz and it seems to have fixed it!
Thanks!
This is a very simple to troubleshoot.
you have used that method and you were able to compile that class which uses this method.
so that means at compile time it reefers the class PlayerUtil which has this method.
But runtime class loader has loaded the class PlayerUtil which doesn't contain this method.
now what you have to do is just find out where that class has been loaded from (at run time)
if you can recreate the problem while it is running using eclipse/IDEA follow these steps.
(if it runs in in application server or standalone application, then start the application server or application with debug enabled.and you can do remote debug from your IDE).
put a break-point where exception was thrown (where you call this method).
start to debug , it will hit the break-point.
then evaluate this expression PlayerUtil.class.getResource("PlayerUtil.class")
4.you can find the path where the class was loaded from.
now you have two options , decompile the class and check whether that method is these (same return type, same name , same args).
or in debug , you can evaluate PlayerUtil.class.getDeclaredMethods() to find out.
So you can solve the problem by rectifying the class path entries if it was loaded from a wrong place.

Must a class name be fully qualified when we already set -classpath to locate that file?

Example:
I have a class called ProgA
package test;
public class ProgA
{
public static void main(String[] args)
{
ProgB pb = new ProgB();
pb.callMe();
}
}
Now I have the ProgB like below:
package test2;
public class ProgB
{
public void callMe()
{
System.out.println("inside callme");
}
}
After compiling ProgB.java its class file is generated in the test2 package. Now when I try to compile ProgA.java using this command:
javac -cp C:\Users\MyName\Desktop\test2 ProgA.java
I get the error that it cannot find ProgB.
My question is why cant java look inside the class path to find ProgB.class file and compile my ProgA.java successfully? The code works fine when I specify the fully qualified class name of ProgB inside ProgA.java code and run with the classpath set to -classpath C:\Users\MyName\Desktop. Why to have the fully quilified name when I am already specifying the full class path to find ProgB. I am not clear with that concept of classpath and fully qualified class name. Please explain me. Thank you
First you would need to import the class. This is why it asks you to use a fully qualified class name. You cannot use a class that is not in the same package without importing it (or using the fully qualified class name).
import test2.ProgB;
Then while compiling, you should provide the class path till the root location, the compiler will look for the class using the package name as the path.
Your compile command should be.
javac -cp C:\Users\MyName\Desktop ProgA.java
In order to use a class from another package, you need to either use the fully qualified class name, or have an import statement. This is a .java source code requirement. It can't be fixed simply by fiddling with the compiler's classpath.
Without an import statement, unqualified names are assumed to belong to the current source file's package. If you're in a package test file, the identifier ProgB will match test.ProgB but not test2.ProgB. The compiler won't search other packages unless you tell it to.

java.lang.NoClassDefFoundError only in particular conditions

My problem is that when class B tries to use A.check() my execution stops due to a java.lang.NoClassDefFoundError.
So here is my class configuration. NB: the classes are in the same packages and I have already checked that the A.class file is placed where it should be.
public class A{
// vars
// declare some public method
public synchronized static boolean check(){
//do stuff, log some info and return boolean
}
}
public class B implements Runnable{
public void run() {
A.check();
}
}
And here is my stacktrace:
java.lang.NoClassDefFoundError:
org/mypackage/A
at org/mypackage.B.run()
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException:
org/mypackage.B
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
The project is really big and class A is used lots of times before this call without any problem, so i don't think that is something relative to the classpath. Note that this is part of the last call of the software that should close up everything.
Moreover, I have two maven goals: the first one execute the questioned code without any problem, instead the second rise this error every time.
So I have solved my problem and I post here the solution so maybe can be useful for someone else.
First of all the error: java.lang.NoClassDefFoundError
This error is really different from ClassNotFoundException and this is where I'have lost a lot of time.
NoClassDefFoundError in Java is raised when JVM is not able to locate a particular class at runtime which was available at compile time. For example, if we have a method call from a class accessing any member of a Class and that class is not available during runtime then JVM will throw NoClassDefFoundError. It’s important to understand that this is different than ClassNotFoundException which comes while trying to load a class at run-time only and the name was provided during runtime, not on compile time. Many Java developer mingles this two Error and gets confused. Here I quote a really useful blog that I uesd.
So in a shorter way NoClassDefFoundError comes if a class was present during compile time but not available in java classpath during runtime.
But even with those information the problem was still there until I found the mystery: one of the reason that can place the class in a state that can be compiled but not located at runtime is that if you have static initialization that fail (e.g. in my class I had as field a static variable instantiated badly).
So remember to check for you initialization phase if you have static variables in your class this could be the reason of your java.lang.NoClassDefFoundError.
By the way I don't get why this kind of error is not raising some more meaninful errors for example java.lang.ExceptionInInitializerError or something like that.
Try to debug maven execution by running: mvn -X <your_goals>
It would be useful to see your POM file.
If you are working with spring mvc and if you made bean entry in dispatche-servlet.xml for Controller class.
Example :
<bean id="MyClass" class="com.aaps.myfolder.MyClass">
<property name="methodNameResolver">
<ref bean="methodNameResolver" />
</property>
</bean>
And if MyClass.java is not compiled & if no class file is generated in classes folder of your project folder then it wil show java.lang.NoClassDefFoundError.
So check whether the MyClass.class is created or not in classes folder if you are working with spring mvc.
Does Class A have anything that is done in a static block. You can get this exception even if a class is being loaded and static blocks fails for any reason reason. try to put in logging to see if something like this is happening.

"constructor has private access" error message

I'm working in Java and have come across an incredibly odd error. I have a very basic class as follows:
public class ClassA{
private static Logger log = Logger.getLogger(ClassA.class.getName());
private boolean trace;
public ClassA(){
trace = log.isTraceEnabled();
}
public void doSomething(){
//does stuff
}
}
I can use this class just fine within my current project. However, when I build, package, and install to my local repo (using Maven, no remote artifact repo set up), other projects cannot properly use this class because they cannot instantiate it. When I try anything like:
ClassA classA = new ClassA();
I get the following compilation error:
ClassA() has private access in [package].ClassA
I've decompiled the .jar in my local repo to ensure the constructor is present and is public - it is. I've also used the -U flag to force updates and the compilation continues to fail. What could be causing this error?
Maybe you have some other ClassA.class file somewhere in the classpath. Check all the jars used by the project that cannot call the constructor: one of them should contain an old version of your class.
My only thought is that you have a problem with your package. Make sure to define the package at the top of the source file for classA using the package keyword. When you call it ensure that the file is in include list with the include keyword. You could be running into the error because ClassA exists in some default package and that is what you are actually calling instead of calling your locally made ClassA class. The code you posted looks fine and you have already double checked to ensure the changes have taken effect in your repository.
//for those with Kotlin-Java mixed projects:
If the said file (With constructor) is in Kotlin and is being used in Java:
Instead of A a = new A(); //which causes the said error
Use A.INSTANCE. …
I have this error, where write "private", instead "public" for class constructor;

Categories