I have a java class I am using in my jruby project and in ruby I can set a value to an object via the send method. I am doing the same in jruby but using the java_send method instead. However, when I try using this I get the following error TypeError: can't convert Java::JavaIo::File into Array from org/jruby/java/proxies/JavaProxy.java:321:in 'java_send'
I have a java instance and I need to call the object via a symbol. Below is what I am doing in the code:
OUTPUT_FILES = [:make, :model]
javaArgs = javaArgs.new
OUTPUT_FILES.each do |filename|
file = java.io.File.new(path, "#{filename.to_s.underscore}.csv")
file.createNewFile
javaArgs.java_send(filename, file)
end
and just to make sure when I do javaArgs.make = file it works without any problems.
java_send expects its arguments passed as an array: javaArgs.java_send filename, [ file ]
Related
When I try to invoke the .NET method – ‘Create’ from Java using Javonet I get a message the method does not exist because I am not passing the correct parameters –
DocuWare.Platform.ServerClient.ServiceConnection
Create(System.Uri,
System.String,
System.String,
System.String,
System.Nullable`1[DocuWare.Platform.ServerClient.DWProductTypes],
System.Net.Http.HttpMessageHandler,
System.Net.Http.Headers.ProductInfoHeaderValue[]
)
My code is –
NObject objUri = Javonet.New("Uri","http://<IP-address>/DocuWare/Platform");
NType serviceConnectionClass = Javonet.getType("DocuWare.Platform.ServerClient.ServiceConnection");
NObject objProductInfoHeaderValue = Javonet.New("System.Net.Http.Headers.ProductInfoHeaderValue","DocuWare+.NET+API+Test+Client", "1.0");
NObject[] objProductInfoHeaderValueArray = new NObject[] {objProductInfoHeaderValue};
NType typeHttpMessageHandler = Javonet.getType("System.Net.Http.HttpMessageHandler");
NType typeNullable = Javonet.getType("System.Nullable");
serviceConnectionClass.invoke("Create",objUri,"admin","admin","<company-name>",typeNullable,typeHttpMessageHandler,objProductInfoHeaderValueArray);
My main problem is not knowing how to generate ‘Nullable’ objects for –
DocuWare.Platform.ServerClient.DWProductTypes
System.Net.Http.HttpMessageHandler
System.Net.Http.Headers.ProductInfoHeaderValue[]
I don't think this is a problem with JavONet, but I need to get past this problem before I can perform a Proof-of-concept
Here is the link to the Docuware Platform –
http://help.docuware.com/sdk/platform-eagle/html/66b2ed1e-2aef-452a-97cd-5014bbf0242b.htm
I am running the Test using Tomcat app server and JSP. I know the .NET .dll are being found and the Javonet library is being correctly activated.
Thanks in advance for any help.
Normally for nullable arguments you can pass either the regular target type in your case some enum value of "DWProductTypes" or if you want to pass null just pass the "null";
So you should make the call:
serviceConnectionClass.invoke("Create",objUri,"admin", "admin","Intermodal Tank",new NEnum("DWProductTypes","DocuWareClient"),typeHttpMessageHandler,objProductInfoHeaderValueArray);
All possible values for DWProductTypes you can find here:
http://help.docuware.com/sdk/platform/html/T_DocuWare_Platform_ServerClient_DWProductTypes.htm
Here you find more about using enumerations:
https://www.javonet.com/quick-start-guide/#Enums_How_To_Work_With_Enums
or pass null:
serviceConnectionClass.invoke("Create",objUri,"admin", "admin","Intermodal Tank",null,typeHttpMessageHandler,objProductInfoHeaderValueArray);
For argument HttpMessageHandler just create the instance:
NObject typeHttpMessageHandler = Javonet.New("SomeConcreteTypeInheritingFromHttpMessageHandler");
For ProductInfoHeaderValue array you should create Java array of NObjects and pass it as argument:
NObject[] objProductInfoHeaderValueArray = new NObject[1];
objProductInfoHeaderValueArray[0] = Javonet.New("ProductInfoHeaderValue","productName","version");
I am trying to write values of jmeter variables into a file using BSF post processor, but am getting an error if I call a variable which has no value
temp6 = vars.get("host_2_g1");
out.write(temp6);
Following is the message I am seeing in the jmeter log file
2015/11/08 21:47:29 WARN - jmeter.extractor.BSFPostProcessor: Problem in BSF script org.apache.bsf.BSFException: BeanShell script error: Sourced file: inline evaluation of: // VALUES is the Reference Name in regex ext . . . '' : vars .get ( "host_2_g1" )
BSF info: [script] at line: 0 column: columnNo
I already know that there is no variable being returned by the name "host_2_g1", how can i handle it so that at-least my code works?
There are multiple problems with your script:
out should be uppercase: OUT
OUT is a shorthand for PrintStream. It won't write strings, is expects byte array, so you need to convert your host_2_g1 variable to byte array via getBytes() method
If your host_2_g1 variable may be not set - it's better to add explicit check.
Modified code:
temp6 = vars.get("host_2_g1");
if (temp6 != null) {
OUT.write(temp6.getBytes());
}
else {
OUT.write("host_2_g1 is null".getBytes());
}
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter.
I have a simple python script in my local machine, which returns a string. I want to run this script from java application and get the return value. I'm trying to do this using Pyrolite. I downloaded the jar files and added them to my java class path. But I'm not able to run the script.
I got the below sample code from readme.txt
NameServerProxy ns = NameServerProxy.locateNS(null);
PyroProxy remoteobject = new PyroProxy(ns.lookup("Your.Pyro.Object"));
Object result = remoteobject.call("pythonmethod", 42, "hello", new int[]{1,2,3});
String message = (String)result; // cast to the type that 'pythonmethod' returns
System.out.println("result message="+message);
remoteobject.close();
ns.close();
But this is not working for me. My system configuration is
OS: Windows 8
JDK: jdk1.7.0_51
Python: 2.6
Please help me with this.
This is how I have edited the code:
NameServerProxy ns = NameServerProxy.locateNS(null);
PyroProxy remoteobject = new PyroProxy();
Object result = remoteobject.call("C:\\trail1.py", null);
String message = (String)result; // cast to the type that 'pythonmethod' returns
System.out.println("result message="+message);
remoteobject.close();
ns.close();
I'm not positive that I understand what you're trying to do.
If you carefully read the tutorial, you'll see that you can't use Pyrolite the way you are. It specifies that you must have a python script running as a server, WITH a name server, where you must define some classes (for example Your.Pyro.Object).
Then you'll be able to call those objects you defined in that python script, but not the script itself.
To do what you want to do you'll need to call a function like C's fork(). Then you're able to call an executable, and you don't need Pyrolite.
I'm trying to extend the example from this tutorial by sending Python objects to Java. While the example code which exchanges String objects between Python and Java works fine, when I try to replace it with my own Python object (Event), an error regarding object_id is displayed.
Python Code:
class Event(object):
#some content here
stack = gateway.entry_point.getStack()
event = Event()
stack.push(event)
Error:
Traceback (most recent call last):
File "/home/******/src/py4jSample.py", line 19, in <module>
stack.push(event)
File "/usr/local/lib/python2.7/dist-packages/py4j-0.7-py2.7.egg/py4j/java_gateway.py", line 423, in __call__
[get_command_part(arg, self.pool) for arg in new_args])
File "/usr/local/lib/python2.7/dist-packages/py4j-0.7-py2.7.egg/py4j/protocol.py", line 241, in get_command_part
command_part = REFERENCE_TYPE + parameter._get_object_id()
AttributeError: 'Event' object has no attribute '_get_object_id'
Any idea how this can be solved?
the problem is that you cannot send pure Python objects to the Java side (in this case, calling push actually calls the Java method "Stack.push"). You can only send (1) objects that can be automatically converted to Java objects (primitives such as int, byte array, strings), (2) objects received from Java such as "stack", or (3) Python objects implementing a Java interface:
class Event(object):
def myMethod(param1, param2):
return "foo"
class Java:
implements = ['yourpackage.IEvent']
If you want to send Python objects implementing a Java interface, you need to accept incoming connections from the Python interpreter (the JVM will call back the Python interpreter if a Python method is called):
gateway = JavaGateway(start_callback_server=True)
stack = gateway.entry_point.getStack()
event = Event()
stack.push(event)
When using java from Matlab, is there some way to figure out from where in matlab's java class path is a class being loaded? I'm trying to diagnose a error caused by conflicting versions of the same class being used simultaneously.
Specifically, the class I'm looking for is org.apache.lucene.store.FSDirectory. It seems to be used by one of the matlab toolboxes, but I don't know which one.
From http://www.exampledepot.com/egs/java.lang/ClassOrigin.html
// Get the location of this class
Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation(); // file:/c:/almanac14/examples/
Assuming that an URLClassLoader is being used, you can get the file: URL of the class file like this:
ProblemClass.class.getResource("ProblemClass.class")
Per Thorbjørn Ravn Andersen, if j reference to a java object in Matlab, its location can be retrieved with the following line of matlab code:
j.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
Use the inmem function as follows:
[M,X,J] = inmem
This function returns the list of Java classes in the output argument J. (It also returns the names of all currently loaded M-files in M, and the names of all currently loaded MEX-files in X.)
Here's a sample of output from the inmem function:
[m,x,j] = inmem;
MATLAB displays:
j =
'java.util.Date'
'com.mathworks.ide.desktop.MLDesktop'
Since 1.5 using:
java -verbose:class
Prints where each class was loaded from.