Android JDK 7 wildcard imports - java

I am experimenting with developing Phonegap plugins for Android using the Phonegap CLI and JDK 7. Time and time again I have found that wildcard imports do not work and I have to explicitly import each class that I need to use. A case in point. I am currently playing with using Rhino to run JS without a WebView. The code samples I find out there suggest import org.mozilla.javascript.* which starts throwing up compiler errors as soon as I put in code that uses the Rhino Context etc objects. In order to be able compile correctly I need to do a whole series of explicit imports. e.g.
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeObject;
Without such imports I end up with error messages along the lines of
cannot find symbol
NativeObject result = (NativeObject)
function.call(rhino,scope,scope,functionParams);
symbol: class NativeObject
Is this a constraint imposed by Phonegap or something that got introduced with JDK 7 (I assume that most of the sample code I am depending on predates JDK 7) or something else altogether. I can see how the Context object in Android can create a conflict here but I am equally certain that I am not accidentally using other classes that define Scriptable, ScriptableObject etc.
I'd be most grateful to anyone who might be able to clarify.

Related

Minecraft Forge 1.16.5 changes since 1.16.4 - compiling error

I am trying to learn Minecraft modding, and while following a tutorial for version 1.16.4, I found that their code did not work, and I presume this is a change to the API, since I am using 1.16.5.
I have looked at both official and unofficial API docs, but these did not provide me any insight. Could anyone point me to a better API reference, or better yet, to a VSCode extension that autocompletes for the most recent Forge API.
Here is the compile error when I ran ./gradlew.bat build with my minimal reproducible example (sorry about the code highlighting, I don't know how to fix it):
C:\Users\eric\Desktop\Programming\Java\Minecraft Mod 1\src\main\java\com\ericl5445\testmod1\core\init\ItemInit.java:1: error: package net.minecraftforge.item does not exist
import net.minecraftforge.item.Item;
^
C:\Users\eric\Desktop\Programming\Java\Minecraft Mod 1\src\main\java\com\ericl5445\testmod1\core\init\ItemInit.java:2: error: package net.minecraftforge.item does not exist
import net.minecraftforge.item.ItemGroup;
^
Here is my full code:
TestMod1.java
ItemInit.java
Any help would really be appreciated!
I do not have an api reference, however I have found that the ItemGroup class is not under net.minecraftforge.item. It is under the package net.minecraft.item.
Your IDE, usually Intellij Idea or Eclipse, should be able to tell you where these classes are located via a search functionality. In Eclipse, you can press Control/Command + Shift + T to bring up a search box with a list of all the classes in your workspace.
The Package You Need To import is net.minecraft.item.Item and net.minecraft.itemGroup,
Not net.minecraftforge.item.Item and net.minecraftforge.itemGroup

Does not exist import android.support.v4.util.ArrayMap;; (What is the easy way to migrate) Does the migration is necessary for any android project

After migrating to android x via android studio getting annotation issues (What is the easy way to migrate) Does the migration is necessary for any android project?
How can I easily migrate the app in android studio.
error: package android.support.annotation does not exist
import android.support.v4.util.ArrayMap;
See the class mappings:
android.support.annotation should rather be: androidx.annotation.
And you probably still have (the wrong one) support libraries referenced; eg:
implementation "com.android.support:support-v4:28.0.0"
Should rather be:
// https://mvnrepository.com/artifact/androidx.legacy/legacy-support-v4/1.0.0
implementation "androidx.legacy:legacy-support-v4:1.0.0"
The same goes for the annotations:
https://mvnrepository.com/artifact/androidx.annotation/annotation/1.1.0
implementation "androidx.annotation:annotation:1.1.0"
all of the references need to be updated.

How to Import MQTT Paho into Java 'playground'?

GOAL: Java to publish an MQTT message through a code playground console. The playground is used to prove out functionality before transplanting instructions to Android Studio.
Reproduce the Error Message in the Playground
After clicking the link to the code playground, click the 'run' button to reproduce the error.
Error Messages
Why does adding the import statements in the code playground?:
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
cause a failure:
./Playground/Playground.java:4: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttClient;
^
./Playground/Playground.java:5: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
^
./Playground/Playground.java:6: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttException;
^
./Playground/Playground.java:7: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttMessage;
^
./Playground/Playground.java:8: error: package org.eclipse.paho.client.mqttv3.persist does not exist
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
^
5 errors
QUESTIONS
Why is it that the resource can not be found?
How can the obstacle be overcome? (substitutes?)
tl;dr - this probably will never work
The Paho Java library is not included with the default classes in the Java SDK. It is what is known as a third-party library.
It ships as a jar file that you need to add to the classpath of any Java runtime you want to use it with.
There are hundreds of thousands such libraries, each offering different extra functionality that extends the default set of standard classes and also available in different release versions.
There is no way the administrators of the code playground could know in advance which of those libraries a user might want to try and just including an import statement at the top of the class doesn't fully identify which version of the library you mean.
While systems like Maven provide a way to look up and download some these libraries in a standard way it's still not suitable for this type of environment and doesn't cover every library you might want.
Also even if the playground did have a way to specify third-party libraries this would open up a huge security problem, because they would have no control over the code that would now run on their machines. I expect the snippets are already run under a security manager that prevents access to the internet and local file system. This would prevent you from being able to connect to the broker.

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: external library.jar.class Android studio

I am developing an android app which process speech and I have speech basic project (dependency for android project) ready on JAVA so I compiled JAVA project in eclipse in JAVA 7 compiler and exported that java project as a runnable jar.
I put this jar into my android studio project's libs folder and by right clicking selected AS A library, I got build successful message. But when I try run the android project it gives me error saying,
FATAL EXCEPTION: main Process: in.automator.automator, PID: 4242
java.lang.NoClassDefFoundError: jar_filename.Storage.class_in_jar_file
but the said class is there in the jar file, the only doubtful thing is the mentioned class file looks something like this
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import marf.util.InvalidSampleFormatException;
public class MARFAudioFileFormat extends AudioFileFormat {
....
...
....
}
It refers javax.sound.sampled, so possibly that might causing the problem.
I tried searching on the google for the problem but didn't got the solution which can resolve the issue. I tried everything.
I am using JRE 7 in android studio and exporting java project in Compiler & itself.
How to resolve this error?
Thanks in advance.
You are correct, the problem lies in the fact that the library you're trying to depend on, in turn depends on the javax package hierarchy. Android does not provide javax, so those classes don't exist at runtime (even though they do exist in your computer's JDK, so your compiler doesn't complain - it's basically the same as not having a particular DLL installed that a program on your PC needs).
The NoClassDefFoundError is being thrown on the class that first references the invalid class dependencies, which is probably what's confusing. That class may indeed actually exist in your jar, but it requires classes that don't exist in order to complete its definition - so in effect, your class is not fully defined.
The only way around this is to figure out a way to do whatever you're after, without the javax namespace. I have heard of a few attempts to try to port javax.* into Android, but they never end well - many of the javax classes end up boiling down to native calls, which also won't exist on Android.

Building reverse engineered project

I learn development for android, I would like to see how certain application is built, I also would like to adjust its UI controls positions for sole use.
So, I decompiled the application and try to build it with Eclipse bundle. I got an apk file built, but when I start it it produces the exception "java.lang.RuntimeException: Unable to get provider iching.android.contentprovider.DivinationProvider"
But I see the file src/iching/android/contentprovider/DivinationProvider.java which contains missing class definition:
package iching.android.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import iching.android.persistence.IChingSQLiteDBHelper;
public class DivinationProvider extends ContentProvider
{
How can I get compiled apk which can't find a class, used in it?
Why does not it find the class declared in properly placed source file?
UPDATE:
I found java compiler was disabled, so bin/classes remain empty on build finish
UPDATE2:
Yep there was a lot of errors when I enabled javac.
I give it a shot to answer your questions but i have to admit its half guessing since i dont see actual results of your decompiling/ running attempts:
0.) Make sure the app (.apk) you have runs as it is (e.g. "it works" as delivered from its producer)
1.) Since decompiling (even in Java) is not trivial as soon as there is complex (or "obfuscated") input data it is very likely the decompiled source will not compile.
=> In 99% of the cases decompiling is used to understand how something work using static code analysis rather than "just run it".
So "the lack of skills" of decompilers CAN lead to what you call "compiled apk which can't find a class, used in it". Mostly however because Class-dependencys were not decompiled correctly.
2.) Because it is not there. It is that simple. Assumed the decompiling worked properly (it looks as it would since you dont have any compile time issues) you are just missing the dependency iching.android.contentprovider. Recheck all of the 3th party librarys are getting attached to your final build-output (i assume a .apk file) correctly. Unfortunately i dont know how you are building your project.

Categories

Resources