Hi I am setting up rserve based on the instructions on this website.
http://www.codophile.com/how-to-integrate-r-with-java-using-rserve/
However in eclipse I am getting an error when using 'eval'
double d[] = c.eval("rnorm(10)").asDoubles();
error - 'The method eval(String) is undefined for the type RConnection'
The JARS have been correctly loaded into the build path and rserve is running, however I cannot figure out why only the eval function is causing an issue.
In addition to this the import of ...
import org.rosuda.REngine.Rserve.RConnection;
causes an error - 'The import org.rosuda.REngine.Rserve.RConnection conflicts with a type defined in the same file'
does anyone have any idea why this is the case? Thanks
all imports :
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class RConnection {
public int[] mean(String a[]) {
setupR();
return null;
}
public void setupR(){
try {
/* Create a connection to Rserve instance running
* on default port 6311
*/
RConnection c = new RConnection();// make a new local connection on default port (6311)
double d[] = c.eval("rnorm(10)").asDoubles();
org.rosuda.REngine.REXP x0 = c.eval("R.version.string");
System.out.println(x0.asString());
} catch (RserveException e) {
e.printStackTrace();
} catch (REXPMismatchException e) {
e.printStackTrace();
}
}
}
By using import org.rosuda.REngine.Rserve.RConnection; you are tying to make RConnection known in the local namespace.
However, you already defined a class called RConnection locally.
Please rename your class RConnection, and you should be able to import org.rosuda.REngine.Rserve.RConnection.
Related
I'm trying to use ObjectBox in a simple java server side app.
Everything is working fine, I'm putting things in boxes etc, but the MyObjectBox class is always red when I use it.
I can see the generated .class and .java files, along with the meta _ classes, in build/classes/main/db (db is the package name I have in my actual code), but for some reason I can't import MyObjectBox.
Because of this, I also can't import the _ classes for use in Queries, which now kind of prevents me from getting any further.
My code to use objectBox is inside a class called DB which I have copied below, in case there's anything I'm doing wrong with that.
But as it actually works, I'm very confused!!
Thanks
UPDATE: if I run gradle clean build, my app runs fine, if I run Build->Build Project in intelliJ then I get the error
Error:(27, 21) java: cannot find symbol
symbol: variable MyObjectBox
location: class DB
.
package db;
import java.io.File;
import java.io.IOException;
import io.objectbox.Box;
import io.objectbox.BoxStore;
public class DB {
private File boxStoreDir;
private static BoxStore store;
public DB() {
try {
createMyObjectBox();
} catch (IOException e) {
e.printStackTrace();
}
}
private void createMyObjectBox() throws IOException {
File objectstorefile = new File("../objectBox/objectstorefile");
if(!objectstorefile.isDirectory()) {
objectstorefile.mkdirs();
}
boxStoreDir = objectstorefile;
if(store == null) {
store = MyObjectBox.builder().directory(boxStoreDir).build();
}
}
public<T> Box<T> getBox(Class<T> object) {
if(store == null) {
try {
createMyObjectBox();
} catch (IOException e) {
e.printStackTrace();
}
}
return store.boxFor(object);
}
}
Forget the apply plugin: 'net.ltgt.apt-idea' in build.gradle
I start Rserve:
C:\Program Files\R\R-3.5.0\bin\x64> "C:\Users\XXXX\DOCUME~1\R\WIN-LI~1\3.5\Rserve\libs\x64\Rserve.exe" --RS-port 1000
Run the following java code:
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class TestR {
private RConnection con;
private RConnection con2;
public TestR(){
try {
con = new RConnection();
con2 = new RConnection();
} catch (RserveException e) {
e.printStackTrace();
}
}
public Double test(){
try {
double d = con.eval("1+1").asDouble();
double c = con2.eval("1+1").asDouble();
return d+c;
} catch (RserveException | REXPMismatchException e) {
return (double)(-1);
}
}
}
I created the following class on JUnit to test it:
import org.junit.Test;
import static org.junit.Assert.*;
public class TestRTest {
#Test
public void test(){
TestR t = new TestR();
t.test();
}
}
When I run this test, it stops while instaciating the connections, it creates the first one, but does hangs on the second. Any idea why this could be happening?
ok
ok
hangs
Your issue might be related to a problem with Multithreading.
Unix: no problem, one Rserve instance can serve mulitple calls.
Windows: Rserve can't create a seperate process by forking the current process.
--> create a new Rserve process for each thread (listening on a different port), a new Rserve connection on the corresponding port has to be established as well.
RConnection connection = new RConnection(HOST, PORT);
If you need more input, do not hesitate to ask. I can also provide my full class code for creating several R instances with Rserve if you want to.
See this Java class I used to run several instances of R with Rserve.
I am migrating my Eclipse RCP to use JDK 8 and I heavily use the JS ScriptEngine. Now that Nashorn is introduced I had to add the following line to get the importClass and importPackage functions to work:
load("nashorn:mozilla_compat.js");
After doing so, I got java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/JSObject.
I am using Nashorn inside an Eclipse RCP. The problem occurs when I call a Java function from the Javascript and try to use the parameter sent. The parameter I want to send is a Javascript function that I would like to execute call on later in the code.
I have the following code:
TestNashorn.java
package com.test.nashorn;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.Invocable;
import jdk.nashorn.api.scripting.JSObject;
public class TestNashorn {
public static void main(String[] args) {
ScriptEngine engine = (new ScriptEngineManager()).getEngineByName("js");
try {
engine.eval(new FileReader("C:/Users/user/workspace_nashorn/TestNashorn/src/com/test/nashorn/test.js"));
Object o = ((Invocable)engine).invokeFunction("generate");
} catch (ScriptException | FileNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
}
}
public static int test(JSObject o1) {
System.out.println(o1.getClass().toString());
JSObject som = ((JSObject)o1);
return 1;
}
}
test.js
load("nashorn:mozilla_compat.js");
importClass(com.test.nashorn.TestNashorn);
function generate()
{
function asd(variablex) { print('Hello, ' + variablex); }
var result = TestNashorn.test(asd);
}
The problem occurs in line JSObject som = ((JSObject)o1);, although I can successfully import jdk.nashorn.api.scripting.JSObject;.
The exception message exactly says:
jdk.nashorn.api.scripting.JSObject cannot be found by com.test.nashorn_1.0.0.qualifier
So.. I got to fix my issue and was able to use JSObject in my code. What I have done was the following:
Added -Dorg.osgi.framework.bundle.parent=ext to myproduct.product file
This added it to the .ini file in my product build which revealed the classes found in Nashorn APIs.
I have a small problem with implementing a own SocketImplFactory in Java.
My goal is to write a factory which offers me a way to close all open sockets with one simple method call. So I only want to have a kind of "proxy factory" which stores all the created sockets in a list. On this list I could perform all the actions I need.
I tried to implement it like this:
package java.net;
import java.io.IOException;
import java.net.SocketImpl;
import java.net.SocketImplFactory;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.crosscloud.applicationlayer.logger.CCLogger;
public class CCSocketImplFactory implements SocketImplFactory
{
private List<SocketImpl> _openSockets;
public CCSocketImplFactory()
{
_openSockets = new LinkedList<>();
}
#Override
public SocketImpl createSocketImpl()
{
SocketImpl impl = new SocksSocketImpl();
_openSockets.add(impl);
return impl;
}
public void closeAll()
{
_openSockets.forEach((socket)->
{
try
{
socket.close();
}
catch (Exception e)
{
logException(this, e);
}
});
}
public static CCSocketImplFactory register()
{
CCSocketImplFactory fact = new CCSocketImplFactory();
try
{
Socket.setSocketImplFactory(fact);
}
catch (IOException e)
{
logException(CCSocketImplFactory.class, e);
}
return fact;
}
The problem I have now is that I have to create the class in the package java.net because the class SocksSocketImpl(In my opinion this should be the standard type) is only visible in this package.
When I now want to run the code I get a SecurityException because the package name is probhibited.
Is there a workaround for my problem?
Thank you!
It appears that you are trying to use only one class from java.net There is no need to move you class tot hat package just to create an instance of it. I suggest using reflection instead.
Constructor cons = Class.forName("java.net.SocksSocketImpl").getDeclaredConstructor();
cons.setAccessible(true);
SocketImpl si = (SocketImpl) cons.newInstance();
However using SOCKS by default is likely to be a bad idea as it will change the default not just for your sockets, but all sockets even ones for internal use such as JMX or VisualVM.
What would be an alternative instead of always using SocksSocketImpl?
I also found this example which shows some extended possibilities of this method.
Finding out what network sockets are open in the current Java VM
I'm using in my program the bluecove library.
While running the program via eclipse, all works smooth. I'm now trying to deploy my program, and following this post i'm using fat-jar.
When i run the jar file (created by fat-jar), the library can't be located, and i'm getting the exception BlueCove libraries not available as result of this line local = LocalDevice.getLocalDevice();.
In the fat-jar window i tried also to add bluecove-2.1.0.jar to the Class-Path place, and also with the path \src\JoJoServer\bluecove-2.1.0.jar.
I tried also to place the bluecove's jar file in different folders, such as the src, or an external folder.
Although i know it's not recommended, i tried the option of One-Jar, nevertheless it didn't help.
To run the jar (the one created by fat jar) i simply double click the file.
What i'm missing?
This is the entire code:
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
#Override
public void run() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
}
// ...
}
I have no clue what could be your problem, but I've tried the process and everything works, so just a summary of what I've did. Maybe you will figure it out by following it...
I don't understand how the posted code could be the entire, I see no class definition. :)
So I've modified it to the main method and it works both from the Eclipse and also by running the JAR generated by the FatJar.
The modified code of the BTTest class:
package test;
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class BTTest {
public static void main(String[] args) throws Exception{
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString()
+ ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier) Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
}
// ...
}
}
To run or produce it, I have just put the bluecove library in the build path and created the fat jar with a simple way:
http://oi60.tinypic.com/vg1jpt.jpg
Starting the generated jar from command line:
D:\testProjects\bttest>java -jar bttest_fat.jar
BlueCove version 2.1.0 on winsock
0000110100001000800000805f9b34fb
BlueCove stack shutdown completed
Can you post a difference to your process?