Can you access a pointer created in c++, in java - java

Is it possible to access a pointer created in c++ in java? Like if I make a string, and make a pointer for the variable (giving the variable a memory place) in c++ is there some command in java that would let me take that pointer and view it? or would I have to output the string to a file, and then preform java file I/O.

You would have to convert it into something java understands through JNI--JNI will have a method to convert your pointer to a string, but then Java will copy the memory and will create a regular Java string out of it--changing your memory after giving it to java will not change the Java string.
I don't think even JNI allows communications through direct memory access but I could be wrong, I haven't looked at it lately.

You should be able to use JNI: http://java.sun.com/docs/books/jni/
You could also try using SWIG: http://www.swig.org/
But before you dive in you should evaluate if you really need to do that. Are you just trying to share data? You could use networking to do that. Pass a tcp message between two programs. Many options exist for sharing data.

Related

Call a java function from a python script

I have a Java script - with a function that I wrote, that I send her a list of strings, the function encrypt each element, and returns a list with the encrypted elements.
My problem is this:
I need to use this function in a python script (send a "list" Python object as input, and receive an "ArrayList" Java object).
How can I call a Java function - that I wrote, in a python script?
And does the list objects are consistent between Python and Java (list Vs. ArrayList)?
A big thank you to all!
** Edit: I'm about to use this entire package in AWS Lambda Function **
The main decisions for choosing a solution seem to be
What do we use to execute the Java program?
How do we transfer computed data from the Java program to the Python program?
E.g. you could decide to use a Java JVM and execute via a call to the operating system from Python.
The computed data could be sent to standard output (in some suitable format) and read in and processed by Python. (See link for the os call and i/o)

Call Minizinc model from Java

How to call a Minizinc model from a Java program with arrays as passed-on parameters?
Is there any special command for doing this?
I frequently do the same but in python. There is probably not any module or extension that can integrate the call in any convenient way but it is quite easy to just call another program.
Since I have not tried it in Java, I will let another stack overflow post guide you: Execute external program in java.
You can pass the parameters either as -D "var_int_name=10;var_int_array=[1,2,3];" or you can supply a data file as the last argument in the call to MiniZinc.
A general tip is to make the output from your MiniZinc model very easy to recognise and parse since many solvers print extra stuff and not just the solution. For example does MiniZinc itself print ---------- between solution. Surround the answer with & or any other sign that is easy to find and parse by a computer. You might also want to verify that you indeed got a solution back.

Using a C++ Struct in Android app (Java and XML)?

I'm a decent C++ programmer, good enough to do what I want. But I'm working on my first Android App (obviously not C++ related), and I'm having an issue where I'd like to translate what I know from C++ over to the XML/Java used in Android Studio.
Basically I have (in C++) an array of structures. And maybe I didn't do the perfect search, but I sure as heck tried to look around for the answer, but I didn't come up with anything.
How would I go about placing an array of structures inside the XML file and utilizing it in Java?
As a bit of a buffer, let me say that I'm not really looking for code, just verification that this is possible, and a method on how to go about it. I don't mind researching to learn what I want, but I haven't come up with anything. Like I said, I probably haven't googled it properly because I'm unsure of exactly how to ask it.
EDIT: So it appears that XML doesn't have a structure (or anything similar? not sure). But I can utilize a Java class with public variables. Now my question is more or less: What would be the best way to go about inserting all the information into the array/class/variables?
In C++ terms, I could neatly place all the info into a text file and then read from it, using a FOR loop to place all the info in the structures. Or, if I don't want to use an outside source/file, I could hardcode the information into each variable. Tedious, but it'd work. I'm not sure, in Android terms, if I could use the same method and pack in a text file with the app, and read from the file using a FOR loop to insert the information into the array/class/variables
class answerStruct
{
public String a;
public boolean status;
};
class questionStruct
{
public String q;
answerStruct[] answer = new answerStruct[4];
};
I'm not placing this here to brag at my super high tech program, but to give a visual, and frankly that's less I have to write out. This is the method I plan on going with. But, being Java, I'm open to possibly better options. My question still stands as far as inputting information into the variables. Hard code? or does Android/Java allow me to place a text file with my app, and read from it into the variables?
XML is just a markup language for tree-structured data, and imposes no restrictions on how you name or structure your tree nodes.
What I think that you're looking for is an XML Object Serialiser: a way to serialise your in-memory structure into XML for a more permanent storage, and then at a later run, deserialise it back into memory. There are many XML Serialisers for Java, each with an own proprietary XML format.
I've used Simple XML in the past, and found it easy and flexible.

accessing static variable in multi threading system

I have to take one input from console in first java program. That input i have to pass in second java program which is getting executed as thread from the main method of first java program.
I made the variable as static and tried accessing in second java program but it is showing null value(default value).
I am not supposed to make the object of first program also.
Please suggest me how to do?
If your first java program is starting the second using O.S.-like functions that produces the same effect as if you were starting from the console, then you will have a second instance of the JVM, with everything "reseted". That's why you're getting that null.
A suggestion would be passing the value as a parameter for the second program.
As far as I know there's no shared memory mechanism implemented in Java. Pipes also don't work across VM boundaries, so you would have to use the JNI and C code to create a mechanism.
But then if the two programs cooperate that close together, why not let them in one VM and use threads? Security reasons is the only thing I could think of.

Pass an array back from a JNI function to without copying it [duplicate]

This question already has an answer here:
Is there any way to pass a Java Array to C through JNI without making a copy of it?
(1 answer)
Closed 5 years ago.
I am trying to use JNI to process large chunks of data using C++ however I am having trouble understanding weather the function SetArrayRegion will duplicate an array element by element or if it can just leave the data in place and return it to the calling java function.
The following documentation is where I have been reading about it but it is still unclear what is going on.
http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
Thank you for the help.
Generally when you pass data via JNI it will be copied across the JNI boundary. If you want an efficient mechanism for passing data from native space up to Java space then you should look at how to access NIO direct byte buffers. This can provide a section of memory that can be shared between native code and Java code. Look at GetDirectBufferAddress.

Categories

Resources