Android: Class in source code not found - java

I am trying to access the PhoneInterfaceManager.java on android.
As it is not part of the sdk, I need to use reflection. (I know I shouldn't do this, but as far as I know there is no other way).
The full name of the class is "com.android.phone.PhoneInterfaceManager".
Now when I run this code...
Object getPhoneInterfaceManager() throws ClassNotFoundException{
return Class.forName("com.android.phone.PhoneInterfaceManager");
}
...I get:
java.lang.ClassNotFoundException: com.android.phone.PhoneInterfaceManager
Note:
I checked the grep repository (which I find to be more reliable sometimes) and noticed that while the package for classes which worked with reflection were in the project "com.google.android / android", this class was to be found in "com.google.android / android-apps ". I don't know if this might mean something, but I thought I'd mention it anyway.
Thanks for your help in advance!

Related

java.lang.ClassCastException: class sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to class com.sun.net.ssl.HttpsURLConnection

Since I updated to Java 11, I am getting an exception which is proving impossible to pin down.
Please note: The classes the exception mentions are nowhere to be found in my own code.
The stacktrace only shows which bit of my code meets the problem, but gives no context at all for the actual code (third party) that causes the problem.
This exception has been asked about before in Stackoverflow, but I can only solve the problem if I can find which third party code is causing this and upgrade it, which will (hopefully) mean that I can see the back of it.
The exception is as follows:
java.lang.ClassCastException: class sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to class com.sun.net.ssl.HttpsURLConnection (sun.net.www.protocol.https.HttpsURLConnectionImpl and com.sun.net.ssl.HttpsURLConnection are in module java.base of loader 'bootstrap')
I strongly suspected some very old apache HttpClient classes, and have replaced them with the java.net.http classes (available since Java 11), but to no avail.
The question is: is there a way to find which .jar contains code that uses HttpsURLConnectionImpl and HttpsURLConnection?
And does anybody know what the rather unhelpful indication that they are in module java.base of loader 'bootstrap' means?
are in module java.base of loader 'bootstrap'
It means that class sun.net.www.protocol.https.HttpsURLConnectionImpl is loaded by the bootstrap loader and are loaded as part of the module base. This is completely useless information: That module is the core java stuff (java.lang.String is in there too), and that loader is the one that loads that stuff. No wonder; sun.* classes are implementation details that are part of what all JDKs ship with out of the box. It provides nothing useful whatsoever for fixing the problem.
The exception is as follows:
There are only two options.
You forgot to paste the stack trace that follows the stuff you did paste. That stack trace is the important part, and lets you know exactly where to look.
You have broken exception handling.
I'm guessing it's option 2 - it's a common thing and perpetuated by manual tutorials. The fix is unfortunate: Go through all your code and fix the bad code. A search for .getMessage() should get you pretty far. It sure sounds like you are doing something like this:
try {
.. code here ..
} catch (Exception e) {
log.warn(e.getClass() + ": " + e.getMessage());
}
or similar. This is always bad - do not just 'log/print' and forget about an exception: You want any exception to be handled, OR thrown onwards. You never want to take an exception whose meaning is not clear as you write the code, and just 'swallow' it. Update your IDE's template settings for generating try/catch clauses; the proper way to write code that just sort of goes; "Exceptions? Um... I do not want to think about those right now / I dont know what they mean / I do know but there's nothing useful I can do about them at this point or I can't fathom how they could occur", which covers most of the time you need to deal with an exception, is like this:
try {
.. stuff ..
} catch (SomethingIDoNotWantToHandleException e) {
throw new RuntimeException("unhandled", e);
}
This will ensure that the place you see that error now has the actual stack trace which will then let you know which library you need to update.
The error was coming from pd4ml, a java library for producing pdfs.
I updated to the latest version (3.11.5), which solved the problem.

Java : Reuse a native library already loaded?

Disclaimer Not native English speaker, feel free to edit if needed.
I'm having a similar issue that the one explained here :
java.lang.UnsatisfiedLinkError: Native Library XXX.so already loaded in another classloader
I'm trying to follow the answer of user2543253. But I really lacks of knowledge in Java and the context is a bit different.
Links
.dll already loaded in another classloader? Seems also related to this question.
https://github.com/PatternConsulting/opencv/issues/7 Similar.
https://cycling74.com/articles/mxj-class-loading Explains the class loader behavior of MXJ
Context
Edit : Not sure if that context is really important, it seems to be the same problem described in link 1.
I want to use OpenCV inside an Application called Max/MSP.
To give an idea, it looks like this :
Max/MSP allows user to assemble Patch by cabling some objects together that are called externals, most of them are coded in C but you can also create externals in Java. To do so you need to instantiate them through an object called "mxj". For example, if my Java class is called TestOpenCV, I will create a box and put "mxj TestOpenCV" inside.
OpenCV seems correctly implemented, for exemple, I can instantiate a Mat object and post its content to Max console.
Problems appears when I change the Java code of the mxj object. To update my object, I delete it and recreate it again. Then, the same issue that explained here appears...
Max console return this error message :
java.lang.UnsatisfiedLinkError: Native Library
C:\Windows\System32\opencv_java300.dll already loaded in another
classloader at java.lang.ClassLoader.loadLibrary1(Unknown Source) at
java.lang.ClassLoader.loadLibrary0(Unknown Source) at
java.lang.ClassLoader.loadLibrary(Unknown Source) at
java.lang.Runtime.loadLibrary0(Unknown Source) at
java.lang.System.loadLibrary(Unknown Source) at
OpenCVClassLoad.loadNativeLibrary(OpenCVClassLoad.java:5) at
TestOpenCV.(TestOpenCV.java:22) (mxj) unable to alloc instance
of TestOpenCV
What I tried
I tried to implement the answer of user2543253. He advices to create a tiny classes that import the native library and export it as a JAR. So I created a new Eclipse project added a source file to it
import org.opencv.core.Core;
public class OpenCVClassLoad {
public static void loadNativeLibrary() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
I added the openCV JAR to that project and exported it as a JAR.
Then I changed my code according to what user2543253 explained (there is more code,I kept the essential) :
import com.cycling74.max.*;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
public class TestOpenCV extends MaxObject {
static {
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
OpenCVClassLoad.loadNativeLibrary();
}
public TestOpenCV(Atom[] args)
{
// ...
}
public void notifyDeleted()
{
// ...
}
public void bang() {
// Executed when I trig the little bang button you can see
Mat m = new Mat(5, 9, CvType.CV_8UC4, new Scalar(0));
post("OpenCV Mat: " + m);
Mat mr1 = m.row(1);
mr1.setTo(new Scalar(1));
Mat mc5 = m.col(3);
mc5.setTo(new Scalar(5));
post("OpenCV Mat data:\n" + m.dump());
}
}
Of course, but that's a bit weird, in order to build correctly that project I kept the JAR from OpenCV in the build path :
As you can see, I also added the tiny class in the project build path.
After all of theses modifications, the mxj object stille loads correctly the first time and the bang() method still works but the problem still there. In fact it doesn't change anything from the past situation : If I modify the Java code, delete the object in Max and create a new one, error appears...
Question
There is a lot of SO questions addressing the same type of prob but context is always different and its hard to figure out what to do, especially with my basic knowledge of Java.
A workaround should be to simply reuse that library already loaded, no ? but how to achieve this ? Because if I check the library has already being loaded, I do it using a Try / Catch, if I do nothing else. The externals acts like if the library had never been loaded...
How to reuse that native library ? (Of course, any alternative solution to this is welcome)
Just remove the second OpenCVClassLoad.loadNativeLibrary(); in your bang() method. In a plain Java application the code in a static block is only executed once.
Alternatively, you can specify the native library location in Eclipse instead of loading the library through Java source code.

java.lang.UnsatisfiedLinkError when trying to follow an MIT example on SWIG usage of C++ callbacks

I am trying to implement a simple application which enables C++ to do callbacks into Java. To this, I found some examples written many years ago by some people at MIT: https://github.com/swig/swig/tree/master/Examples/java/callback
However, when trying to run my own application, I get
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.swig.demo.SwigDemo3JNI.swig_module_init()V
at com.swig.demo.SwigDemo3JNI.swig_module_init(Native Method)
at com.swig.demo.SwigDemo3JNI.<clinit>(SwigDemo3JNI.java:30)
at com.swig.demo.Caller.<init>(Caller.java:39)
at com.swig.demo.SwigTest.main(SwigTest.java:47)
Where SwigDemo3JNI is the Swig generated Java class, and SwigTest is the java class with main. Could anyone give me some insight on what I'm doing wrong?
You can set library path when init the app, the code below is used to do this:
String libraryPath = "c:/your/path/to/dll/lib";
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
System.loadLibrary("the_dll_lib_name");
Hope this help you!

java cant find or load my program when I have a package heading

I can run programs which do not have a package without any hitch. If I try and add a package then java simply cannot find them. I have set the class path and I have tried running - java packagename.ProgramName.
I have found a number of similar threads on here and have spent four hours going through all of them and trying everything and nothing works for me.
Yet as soon as I edit the .java file and recompile without a package heading - it immediately works perfectly. Why? And how can I fix it? I would like to be able to have my classes organised in packages!
This is the code I am using (I normally use eclipse and just wrote this to try out cmd out of curiosity).
package hello;
public class HelloWorldApp{
public static void helloWorld(){
System.out.println("Hello world");
}
}
and
package hello;
public class HelloBackApp{
public static void helloBack(){
System.out.println("Hello back");
}
public static void main(String[] args){
HelloWorldApp.helloWorld();
helloBack();
}
}
As I say if I delete both the package heading java HelloBackApp runs just fine.
And my path to my program is
c:\Users\sam\javastuff\hello
I have of course tried java hello.HelloBackApp from both the javastuff dir and the hello dir. No joy
It works immediately if I delete both the package headings and type java HelloBackApp from the hello directory.
try as follows,
create folder structure as your package and place java file in that folder
For ex, my java file is under
c:\code\com\test\Test.java and package is "package com.test".
I compiled and run code from
c:\code>
c:\code> javac com\test\Test.java
c:\code> java com.test.Test
Ok after much research I realised what my problem was and have fully resolved it. I think I see why I have been unable to find an "answer" to this question in forums. It is not a simple quick fix - my whole understanding of how to correctly get the class path set up and get a proper compile done was very poor. It becomes a whole new subject if you switch from compiling/running on an IDE to doing so from the command line. I think it is an excellent thing for new programmers to do though as I believe the improved understanding of CLASSPATH is going to be something that will stand us all in good stead for the future.
I found all the answers to my questions here : http://www.ibm.com/developerworks/java/library/j-classpath-windows/
and recommend anyone having similar problems I was having to read through this excellent document. Best wishes to all the other guys struggling with this out there! :)

Another Netbeans issue | cannot find symbol method, but correctly declared

this has been a crappy day, besides the IDE not compiling/deploying because of this bug and waisting valuable time, I finally get it to deploy it suddenly I start getting this weird message (after compiling and running it several times):
T:\Users\Triztian\Documents\RHT System\RHTUBSDB\src\java\controllers\OrderSearch.java:64: cannot find symbol
symbol : method metadata(java.lang.Long)
location: class BO.CoverForm
OrderExtraInfoDTO foundInformation = frmCover.metadata(foundOrder.getReferenceNumber());
it is my understanding that this means that my method isn't declared, but thats not the situation as my method is clearly declared and coded.
CoverForm.java:
public OrderExtraInfoDTO metadata(Long ReferenceNumber) {
OrderExtraInfoDTO foundInformation = new OrderExtraInfoDTO();
try{
foundInformation = lnkAddInformation.fetchInformation(ReferenceNumber);
} catch (DAOException daoe) {
this.setError("additional_information", daoe.getMessage());
}
return foundInformation;
}
And the servlet that calls the CoverForm.java method.
OrderSearch.java (Extends HttpServlet):
CoverDTO foundCover = frmCover.search(foundOrder.getReferenceNumber());
OrderExtraInfoDTO foundInformation = frmCover.metadata(foundOrder.getReferenceNumber());
UpgradesDTO foundUpgrades = frmUpgrades.search(foundOrder.getReferenceNumber());
I've tried renaming the method and didn't have any success, any help is truly appreciated as I'm getting frustrated with NB 6.9.1 because of some crashes and another weird bug (might catch an entomologist's attention) which locks the editor and displays a message saying: "Refactoring cannot be done in the given context" whenever I press delete, forcing me to restart the IDE.
EDIT
Ok, so I've removed the classes that I posted and merged them in a more appropriate place, however I still get that silly symbol not found error but on a different symbol(another method) this time.
Netbeans 6.9.1 is a very robust IDE. You may run into problems like the one you mention above, if:
You run your NB without enough disk space available. Make sure that you have at least 2 GB free on your file system for the necessary temporary files.
You have a very large number of projects active in your project space. Reduce this number to just the needed projects, by deleting and reopening more often.
Hope this helps ...

Categories

Resources