Java Scripts

Ok, I’m porting some code over from java, and again I’m haing a problem figuring out what to pass in for parameters. In the following code, I get the “readAsBytes() takes at least 2 arguments (1 given)” error. How can I tell what it’s looking for?

from java.lang import Byte
from java.io import FileInputStream
from java.io import File
import jarray
import java.net

def readAsBytes(self,file):
	print 'ok'


url = java.net.URL("http://localhost:8088/Import/Test.txt")

B=readAsBytes(url)

What are you wanting the code to do? If you want to read the file as an array of bytes, there is an Ignition function to do this - system.file.readFileAsBytes(filepath).

Well your function has two arguments self and file. Just remove the self since it is not needed.

Nope, I wasn’t aware of that. That just might do the trick. But, the point still stands- what is this looking for? I think it’s looking for a File object, and I tried instantiating one by passing in various flavors of java.io.newInstance(), but no-go. Just want to understand this a little better. (I’ve added a line of code in the function, and the rest will work if it gets past the File object).

from java.lang import Byte
from java.io import FileInputStream
from java.io import File
import jarray
import java.net

def readAsBytes(self,file):
	f = File(file)

url = java.net.URL("http://localhost:8088/Import/Test.txt")

B=readAsBytes(url)

I tried that too, but then the “f = File(file)” fails. As I mentioned above, I think it’s looking for a file object.

Yes it will because the function does not have the Java File imported. You need to place the imports inside the function:[code]from java.lang import Byte
import jarray
import java.net

def readAsBytes(file):
from java.io import File
from java.io import FileInputStream
f = File(file)

url = java.net.URL(“http://localhost:8088/Import/Test.txt”)

B=readAsBytes(url)[/code]

Yep, removing “self” and adding the imports did the trick. Thanks!