Java Me 8 raspberry pi - java.security.AccessControlException - java

I have the following simple code written in java-ME embedded:
public class JavaMEApplication2 extends MIDlet {
#Override
public void startApp() {
GPIOPinConfig config1 = new GPIOPinConfig(DeviceConfig.DEFAULT, 4, GPIOPinConfig.DIR_OUTPUT_ONLY,
DeviceConfig.DEFAULT , GPIOPinConfig.TRIGGER_NONE, true);
try {
GPIOPin pin = (GPIOPin) DeviceManager.open(config1);
Thread.sleep(2000);
pin.setValue(false);
pin.setDirection(GPIOPinConfig.MODE_INPUT_PULL_UP);
} catch (IOException ex) {
Logger.getLogger(JavaMEApplication2.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(JavaMEApplication2.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void destroyApp(boolean unconditional) {
}
}
The previous code run just fine (My LED is turned ON and OFF) until the execution reach this statement:
pin.setDirection(GPIOPinConfig.MODE_INPUT_PULL_UP);
The following exception occurs:
TRACE: <at java.security.AccessControlException: >, startApp threw an Exception java.security.AccessControlException:
My API permissions Configuration:
Can any one please tell me why this exception occurs? and if there's another way to toggle the same pin between OUTPUT Mode and INPUT Mode in java-Me embedded?

I am running into the same problem when i try and use the ADSONG AM2302 temp and humidity sensor, What I have done to avoid that is connect another pin with a pull up resistor to the current pin that is initially input/output and set one pin to output and one to input, it gets rid of the permission problem at least. The sensor is still not responding to my start signal though so this may have cause unforeseen problems

I think it may be failing because you had this parameter in the call to the GPIOPinConfig constructor:
GPIOPinConfig.DIR_OUTPUT_ONLY
Maybe try GPIOPinConfig.DIR_BOTH_INIT_OUTPUT instead.
Look here for the different values and their meanings:
https://docs.oracle.com/javame/8.0/api/dio/api/index.html

open your jwc_properties.ini file from /home/pi/javame8/bin directory inside raspberry pi and add:
authentication.provider = com.oracle.meep.security.NullAuthenticationProvider
under [internal] section

Related

Spark submit in java(SparkLauncher)

I made spark+hadoop yarn enviroment and spark-submit command works well. So I made SparkLauncher java code to do this in my application jar, BUT somehow it doesn't work (actually computer fan is spinning at first but not as long as i did with spark-submit.)
It seems not work well (no application log in hadoop web ui, unlike spark-submit). I cannot see any error log when I do with 'SparkLauncher'. without log message, I can do nothing with it.
Here is how I made it so far.
public class Main {
public static void main(String[] args) {
Process spark = null;
try
{
spark = new SparkLauncher()
.setAppResource("/usr/local/spark/examples/jars/spark-examples*.jar")
.setMainClass("org.apache.spark.examples.SparkPi")
.setMaster("yarn")
.setDeployMode( "cluster")
.launch();
}
catch( IOException e)
{
e.printStackTrace();
}
}
}
executed it with ( java -jar example.jar)
I had the same problem at first. I think the main issue is that you forgot about the waitFor().
Also, it's really helpfull to extract your errorMessage and deal with it (e.g. log it or checking it while debuging ) within your java code. To allow this, you should create a streamReader thread as follows:
InputStreamReaderRunnable errorStreamReaderRunnable = new InputStreamReaderRunnable(spark.getErrorStream(), "error");
Thread errorThread = new Thread(errorStreamReaderRunnable, "LogStreamReader error");
errorThread.start();
int result= spark.waitFor();
if(result!=0) {
String errorMessage = extractExceptionMessage(errorStreamReaderRunnable.getMessage());
LOGGER.error(errorMessage);
}
This should be after your launch() command and inside your try block. Hope it helps

I want to get application crash info in Android programmatically without using 3rd party apps or jars like bugsense etc

I have a webview based android application I want to get the crash report and store it into a text file but i am unable to find any programmatic native solution anywhere. I have read this article but I am unable to use it. I have no code because I dont know where to start
The best way to handle crash logs is creating an UncaughtExceptionHandler and handling it as per your requirement. Create a BaseActivity class and extend all the Activities with that and put this code stuff in the BaseActivity class.
private Thread.UncaughtExceptionHandler handleAppCrash =
new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("error", ex.toString());
//send email here
}
};
Then just enable is inside onCreate() method of your BaseActivity by using
Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);
I am catching un-handled exceptions by using this in my activity's onCreate():
mUEHandler = new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
try {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(
openFileOutput(DMP_FILENAME, 0)));
e.printStackTrace(pw);
pw.flush();
pw.close();
} catch (FileNotFoundException e1) {
// do nothing
}
BaseActivity.this.finish();
}
};
Thread.setDefaultUncaughtExceptionHandler(mUEHandler);
This writes every unhandled exception in your app that happened on your activity to text file. Then you can analyze it.
Why don't you log every thing like Log.e("tag","msg") or else check the logcat for the crashes

Java Eclipse "process" not ending after closing application

I am writing an application that displays webcam connected to my computer.
I will just write code here since the code is very simple.
public static void main(String[] args) {
JFrameImageDisplayer _window = new JFrameImageDisplayer();
//webcamGrabber _wg = new webcamGrabber();
//commented out because I am having trouble with this class.
}
JFrameImageDisplayer opens a frame, pretty much that's all it does.
When I run this code, I open a simple application with a JLabel in the frame. If I close the application, then the whole process terminates( and the process at the Windows Task Manager Process tab processes as well).
However once I create _wg, the process at the Task Manager does not terminate even after I close the application ending up just burning processing power until I manually go to process bar to end it.
Below is the construction code for the webcamGrabber.
{
OpenCVFrameGrabber _grab = new OpenCVFrameGrabber(0);
try{
_grab.start();
} catch (Exception e){
e.printStackTrace();
}
}
Well I was not so sure what do. So I manually released the resources.
protected void processWindowEvent(WindowEvent e){
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
try{_wg._grab.release();}
catch(Exception ee){}
}
super.processWindowEvent(e);
}
Not the prettiest way to do it, but it works.

Catch all type exceptions programming Android

I'm developing an application for Android OS.
Since this is my first application, I think I've committed some programming mistakes cause I hardly can trace bugs back to their causes.
Thus, I was guessing, while i'm trying to fix bugs, is there a way to catch ALL types of exception in my entire activity lifecycle with one try-catch?
That would be awesome, i'm getting bored watching my galaxy S say :"Sorry the application App has stopped unexpectly" :(
I really, really don't recommend this...
try {
...
} catch (Exception e) {
// This will catch any exception, because they are all descended from Exception
}
Are you looking at your stack traces to debug your issues? It should not be hard to track them down. Look at LogCat and review the big block of red text to see which method caused your crash and what your error was.
If you catch all your errors this way, your program is not going to behave as expected, and you will not get error reports from Android Market when your users report them.
You can use an UncaughtExceptionHandler to possibly prevent some crashes. I use one, but only to print stack traces to a file, for when I'm debugging an app on a phone away from my computer. But I pass on the uncaught exception to the default Android UncaughtExceptionHandler after I've done that, because I want Android to be able to handle it correctly, and give the user the opportunity to send me a stack trace.
I'm assuming like pure java
try {
} catch(throwable t) {
}
But this is Very Bad Practice.
Also look at
Setting UncaughtException handler
If you're on Eclipse, every exception that Force-Closes the app (aka the message you mention) should be logged in the "LogCat".
The easiest way to see the LogCat, is to Open the DDMS perspective and clic on the LogCat tab (or open it from the "View" menu if it's not already displayed).
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
public class SRSDexception implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler defaultUEH;
private Activity app = null;
public SRSDexception(Activity app) {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
this.app = app;
}
public void uncaughtException(Thread t, Throwable e)
{
StackTraceElement[] arr = e.getStackTrace();
String Raghav =t.toString();
String report = e.toString()+"\n\n";
report += "--------- Stack trace ---------\n\n"+Raghav;
for (int i=0; i<arr.length; i++)
{
report += " "+arr[i].toString()+"\n";
}
report += "-------------------------------\n\n";
// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report += "--------- Cause ---------\n\n";
Throwable cause = e.getCause();
if(cause != null) {
report += cause.toString() + "\n\n";
arr = cause.getStackTrace();
for (int i=0; i<arr.length; i++)
{
report += " "+arr[i].toString()+"\n";
}
}
report += "-------------------------------\n\n";
try {
FileOutputStream trace = app.openFileOutput(
"stack.trace", Context.MODE_PRIVATE);
trace.write(report.getBytes());
trace.close();
} catch(IOException ioe) {
// ...
}
defaultUEH.uncaughtException(t, e);
}
}

How to Change Screen Resolution Programmatically?

Let me tell you my problem. I want to change my screen resolution.
I can change it in an application but it changes only application's
screen. I wanna set system's resolution so it won't be important which
application is running on front. My device's resolution is set as 1280
* 720 p. Can I make it 1260 * 680? If it requires to make changes in
Android source code, I can. Just tell me where to change. Waiting for
your help.
This thread on xda-developers should set you on the right track.
Searching too a valid answer to this, but I have a lead to the solution :
WARNING Experimental buggy stuff :
/*
Requires android.permission.WRITE_SETTINGS
and android.permission.WRITE_SECURE_SETTINGS, thus it requires the app to be a system app.
*/
public void changeResolution(int x, int y){
try { Class c = Class.forName("android.os.ServiceManager");
try { Method method = c.getDeclaredMethod("checkService", String.class);
try {
IWindowManager mWindowManager = IWindowManager.Stub.asInterface((IBinder) method.invoke(null,Context.WINDOW_SERVICE));
try { mWindowManager.setForcedDisplaySize(Display.DEFAULT_DISPLAY,x,y);
} catch (RemoteException e) {e.printStackTrace();}
} catch (IllegalAccessException e) {e.printStackTrace();}
catch (InvocationTargetException e) {e.printStackTrace();}
} catch (NoSuchMethodException e) {e.printStackTrace();}
} catch (ClassNotFoundException e) {e.printStackTrace();}
}
Add a reduced AIDL version of IWindowManager to your project :
/app/src/main/aidl/android/view/IWindowManager.aidl
package android.view;
interface IWindowManager
{
boolean startViewServer(int port); // Transaction #1
boolean stopViewServer(); // Transaction #2
boolean isViewServerRunning(); // Transaction #3
void setForcedDisplaySize(int displayId, int width, int height);
void clearForcedDisplaySize(int displayId);
void setForcedDisplayDensity(int displayId, int density);
void clearForcedDisplayDensity(int displayId);
}
The app will require to be in the system apps folder.
It does something for sure, but right now it also lead to severe bugs.
Rebooting seems to cancel changes.
Waiting for feedback on this.
If you are using Windows and know how to use JNI, Microsoft provides C++ Win32 function calls to do this: ChangeDisplaySettingsEx() and EnumDisplaySettings().

Categories