Using SCPSolver in Android - java

I am trying to use SCPSolver in Android project to solve linear programming problem.
I have downloaded the Jar file and added it to the lib folder. Everything works seemingly fine until I invodel the solve method; I get a null pointer exception.
This is my code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import scpsolver.constraints.LinearBiggerThanEqualsConstraint;
import scpsolver.constraints.LinearSmallerThanEqualsConstraint;
import scpsolver.lpsolver.LinearProgramSolver;
import scpsolver.lpsolver.SolverFactory;
import scpsolver.problems.LinearProgram;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearProgram lp = new LinearProgram(new double[]{5.0,10.0});
lp.addConstraint(new LinearBiggerThanEqualsConstraint(new double[]{3.0,1.0}, 8.0, "c1"));
lp.addConstraint(new LinearBiggerThanEqualsConstraint(new double[]{0.0,4.0}, 4.0, "c2"));
lp.addConstraint(new LinearSmallerThanEqualsConstraint(new double[]{2.0,0.0}, 2.0, "c3"));
lp.setMinProblem(true);
LinearProgramSolver solver = SolverFactory.newDefault();
double[] sol = solver.solve(lp);
}
}
This is the error I get:
java.lang.RuntimeException: Unable to start activity
Attempt to invoke interface method 'double[] scpsolver.lpsolver.LinearProgramSolver.solve(scpsolver.problems.LinearProgram)' on a null object reference
....
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'double[] scpsolver.lpsolver.LinearProgramSolver.solve(scpsolver.problems.LinearProgram)' on a null object reference

I did that now I am getting :
Installation failed due to: 'INSTALL_FAILED_NO_MATCHING_ABIS:
Failed to extract native libraries, res=-113'
The standard SCPSolver JARs do not contain native libraries for Android platforms. The Website says that the downloadables support "Linux, Mac Os X and Windows". Android is not listed.
You could attempt to port SCPSolver to Android:
fetch the source code from BitBucket (https://bitbucket.org/hplanatscher/scpsolver.git)
modify one of the existing solver build script to work with Android
run it.
You would also need to deal with any Java(TM) vs Android incompatibilities that affect the codebase.
However ... my Googling didn't reveal any evidence that anyone has succeeded in doing that, so there could be technical problems that need to be overcome.
The other possibility would be to pay someone to do the porting work for you. Note that you / they would need to comply with the terms of the applicable copyright licenses. See the Website for details.

Related

CodenameOne Java String.Format "error: cannot find symbol" on Hello World example

A super-simple String.format("this is a test %d",5) doesn't work in my HelloWorld CodenameOne project: I get "error: cannot find symbol".
It doesn't seem to matter what format I used, I always get the same error. This seems to be an import problem, though I'm not importing any special packages outside of the defaults.
Here is the java source:
package com.test.test;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
// use two network threads instead of one
updateNetworkThreadCount(2);
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
/*
Updating property file: C:\Users\admin\Desktop\test2\build\built-jar.properties
Compile is forcing compliance to the supported API's/features for maximum device compatibility. This allows smaller
code size and wider device support
Compiling 1 source file to C:\Users\admin\Desktop\test2\build\tmp
C:\Users\admin\Desktop\test2\src\com\test\test\MyApplication.java:39: error: cannot find symbol
s = String.format("this is a test %d",5);
symbol: method format(String,int)
location: class String
1 error
C:\Users\admin\Desktop\test2\build.xml:62: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
*/
String s;
s = String.format("this is a test %d",5);
addNetworkErrorListener(err -> {
// prevent the event from propagating
err.consume();
if(err.getError() != null) {
Log.e(err.getError());
}
Log.sendLogAsync();
Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
});
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
CodenameOne compiles your source code using its own subset of the Java SE API, which is missing some features that the standard Java API includes.
Quoting their FAQ:
What features of Java are supported? What features of Java aren't supported?
The most obvious thing missing is reflections. The main problem is that when we package the VM into devices that don’t have Java, we would have to include EVERYTHING. If reflections were included, they wouldn’t work anyway since we obfuscate the code for the platforms where reflections do work (e.g. Android). On top of that reflection code is generally slow and a bad idea on a mobile device to begin with. As an alternative some developers were successful with bytecode manipulation which is something that is completely seamless to the server and as performant/efficient as handcoding.
Many of the desktop API’s such as java.net, java.io.File etc. aren’t very appropriate for mobile devices and just didn’t make it. We provide our own alternatives which are more portable and better suited for mobile settings.
Of the other missing things, if you run into a missing method or ability, there are cases where that functionality can be added.
Specifically, its version of java.lang.String does not include the format method.
In this case, it can be rewritten using simple string concatenation:
String s = "this is a test " + 5;

Why do I get "LoadLibrary failed with error 1114: a dynamic link library (DLL) initialization routine failed"?

When I run my java application program an error window appears saying that
"LoadLibrary failed with error 1114: a dynamic link library (DLL) >initialization routine failed".
I have tested my code on a different machine and it worked perfectly.The program shows a PApplet window with a map inside.However, Running the code on my laptop, the PApplet appears and all of the sudden the DLL error stops the rest from being shown.
What the problem could be and how can I fix it?
Here is the code I am trying to run. It is worth to mention that it runs successfully if I remove what's inside the setup() method.
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.utils.MapUtils;
import processing.core.PApplet;
public class LifeExpectancy2 extends PApplet {
UnfoldingMap map;
public void setup()
{
size(800,600,OPENGL);
map = new UnfoldingMap (this, 50, 50, 700, 500, new Google.GoogleMapProvider());
MapUtils.createDefaultEventDispatcher (this, map);
}
public void draw()
{
}
}
I had the same issue after I installed my Netbeans to build some projects in PHP and it was fixed changing some graphics options in the control painel of my Windows 10.
Take a look on this video and see if it fixs your issue as well:
Windows 10 - Java Loadlibrary Error 1114
I hope it can be helpful!
Which Unfolding version did you download? You seem to use some Java IDE (and not Processing's one) so you need the Unfolding-for-Eclipse distribution which includes all needed native libraries (i.e. also the DLL in question).
For the records, the DLL is the native library for Windows OS to bind Java to the OpenGL API (JOGL).

Running java program by considering import dependencies

I have java file at location.
/root/Desktop/software/UIMA/yagogit/yodaqa/src/main/java/cz/brmlab/yodaqa/analysis/question/FocusGenerator.java
This file is part of entire project - FocusGenerator.java
it is importing couple of classes from UIMA and few other packages. (I already configure UIMA on my system)
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.UimaContext;
import org.apache.uima.resource.ResourceInitializationException;
import cz.brmlab.yodaqa.model.TyCor.LAT;
import cz.brmlab.yodaqa.provider.OpenNlpNamedEntities;
import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS;
import de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity;
While executing entire projects following readme file, it works well. But I wanted to test each individual program, like one mentioned above. When I try to compile using javac it gives error, cannot find symbol as below
ATByFocus.java:77: cannot find symbol
symbol : class ImplicitQLAT
location: class cz.brmlab.yodaqa.analysis.question.LATByFocus
addFocusLAT(jcas, focus, "amount", null, 33914, 0.0, new ImplicitQLAT(jcas));
^
LATByFocus.java:83: cannot find symbol
symbol : class LAT
location: class cz.brmlab.yodaqa.analysis.question.LATByFocus
addFocusLAT(jcas, focus, text, pos, 0, 0.0, new LAT(jcas));
and so on.
What is correct way to execute this file. I tried it importing in eclipse to, but in eclipse too it could not be imported as project.
It is difficult to build pieces of YodaQA in isolation. I think it's much simpler to just work within YodaQA, but create your custom main class which will directly call the FocusGenerator or any other class you want.
To add another main class and execute it, you will need to add another gradle target. See build.gradle for a few examples already: tsvgs, biocrftrain, etc.

UnsatsifiedLinkError on JSSC Serial Interface (Processing)

I'm using Proclipsing (processing in Eclipse) but am getting an error when I try and open a port (printing the serial list works fine). I have a feeling some sort of native library is not connected but I'm baffled as to how to do so in Eclipse (and where it link to).
Here's my code:
import processing.core.PApplet;
import processing.serial.Serial;
public class visualization extends PApplet {
public Serial usb = null;
public void setup() {
println(Serial.list());
println(Serial.list()[5]);
usb = new Serial(this, Serial.list()[5], 115200);
}
public void draw() {
}
}
and the error it throws is:
Exception in thread "Animation Thread" java.lang.UnsatisfiedLinkError: jssc.SerialNativeInterface.openPort(Ljava/lang/String;Z)J
at jssc.SerialNativeInterface.openPort(Native Method)
at jssc.SerialPort.openPort(SerialPort.java:158)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at bioauthvisualization3.BioauthVisualization3.setup(BioauthVisualization3.java:15)
at processing.core.PApplet.handleDraw(PApplet.java:2361)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Thread.java:745)
I ran into the same issue while setting up IntelliJ IDEA for development with Processing. But I imagine you can apply the same method in Proclipsing. The solution that worked for me was threefold.
I created a global library that contained the Processing and Serial libraries. In OS X the required jar files can be found in the directory /Applications/Processing.app/Contents/Java/ and /Applications/Processing.app/Contents/Java/modes/libraries/serial/library/. I added the following libraries: core.jar, serial.jarand jssc.jar.
Next I had to add the global library to my module dependencies.
The last step was to add the path to the native libraries to the VM options for the appletviewer: -Djava.library.path="/Applications/Processing.app/Contents/Java/modes/java/libraries/serial/library/macosx"
My guess is that Proclipsing does the first two steps for you but you have to add the native libraries to the vm options manually since these depend on the system you're working on. Hope that helps.
I derived the solution for step 3 from this forum entry.

App running crash with this error: could not find class external jar Android [duplicate]

I am trying to create a simple test app that basically extends the Android Hello World tutorial app by invoking some simple functionality from an external JAR. However, when I run the app, it can't find the class from the JAR. What am I doing wrong?
Here's entire source of the JAR:
package com.mytests.pow;
public class power2 {
private double d;
public power2()
{
d = 0.0;
}
public String stp2(double dd)
{
d = dd*dd;
return String.format("%e", d);
}
}
And here's the "Hello World ++" source:
package com.myLuceneTests.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.mytests.pow.*;
public class AndroidSimpleSIH_EclipseActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
power2 pp = new power2();
String iout = pp.stp2(12.0);
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText(iout);
setContentView(tv);
}
}
When I run the app, I get this in logcat:
11-22 12:24:52.697 2963 2963 E dalvikvm: Could not find class 'com.mytests.pow.power2', referenced from method com.myLuceneTests.namespace
.AndroidSimpleSIH_EclipseActivity.onCreate
and then
11-22 12:24:52.713 2963 2963 E AndroidRuntime: java.lang.NoClassDefFoundError: com.mytests.pow.power2
What am I doing wrong? Thanks!
By the way, my actual goal is to use a real JAR (rather than this toy one) in an Android app. I might have access to the code for that JAR and might be able to rebuild it but it's a big piece of Java code and I am likely to encounter problems when rebuilding it so I am trying to use the pre-built JAR.
There's no need to build the external jar. Try removing it from your build path and follow these steps:
Create a folder in the root of your Android project called libs.
Add the jar to that folder.
Right-click the jar and click to add to build path.
Clean your project and try it again.
i. Generate Jar for your external Classes
ii. Copy to Libs folder of your Android Project
iii. Open Eclipse->Right Click on Jar->Add To Build Path.
iv. Clean Project and Run Application.

Categories