Uses of Java Native Interface(JNI) [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Could you please help me understanding under what scenario JNI (or Java Native Interface) could be useful from web application development perspective?
I did read some articles that promises that JNI can give you access to native application like menu bar/scroll bar etc, so the user interface can be more catchy... however these things can be done in other technologies rather than Java.
I'm still not able to find practical uses of JNI. Have you used the JNI in your projects/apps ever? and for what?

JNI is useful if you need to call some lower level language (C, C++, assembler) function from within Java. Many projects use JNI, for example:
JOGL (Java library for making calls to OpenGL, a C library)
SWT (Java UI library that uses JNI to call a native windowing library, for example GTK on Linux)
There are utilities to automatically generate JNI code, like SWIG, which can dramatically ease the pain of generating JNI wrapper code (which involves writing a bit of Java and some of the underlying language).

It shouldn't be necessary for web development since just about anything you need is already written in Java--and it would also be a terrible idea because it will make your app platform dependent.
It is sometimes used to access a system API an old DLL that you must use without rewriting for some awful management-related reason.

Related

How to build and maintain a library for multiple languages? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to implement a library which will be called from multiple languages - Java, Python, Perl and possibly more in the future. I'd hate to implement (and maintain) the same thing again and again in multiple languages.
One option I can think of is to write the core functionality in C/C++ and use SWIG to generate bindings for target languages; or maybe write the bindings myself.
Thre are some reasons why it can't be an independent service.
Are there any other mature alternatives? I am looking for options to compare before I settle for one.
Ideally, I'd like to do it using a source to source compiler or a source code generator. But I can't find one that supports all of the above languages - with potential for future additions.
I may consider any alternative binding generators if they provide any advantages over SWIG.
Thanks in advance for any pointers!
The simplest way is to write the library in C with a simple API. Every non-obscure language has some way to interface with C code.
Depending on the style of the API, SWIG can save some time in generating the bindings; but unless it's very big and regular, you might find it easier to write the bindings by hand.
Some Languages (at least Python in your question) have an FFI mechanism, that lets you write the whole binding in the target language, making it much easier to deploy and maintain. Note that most of those are focused on C APIs, not C++.
Depends on how you plan on access the library, is it possible to expose the public API via Web Services?
This way you can write the shared module in any language and access it via a common protocol such as a RESTfull web service.
Look at Microservices architecture.

Add Java GUI to C client [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have written a fully functional chat program in C. The server starts up and allows multiple clients to connect and talk to each other, all via command line. However I would like to try adding a GUI to this program, but all of the C/C++ GUI packages seem to have a steep learning curve just to get started. Is there any way to write a GUI using Swing and connect it to the C code underneath?
For example, I would like to type some text into a box, click "send" and have it call the C function which deals with sending text. Is this possible? And if so, is it very difficult?
I can provide code if needed. I am also open to suggestions on which C++ GUI package might be most appropriate for this kind of program.
Yes, its possible. You would have to use JNI. You should really consider the learning curve of doing JNI vs learning whatever GUI framework you want to use you C/C++.
As someone who has done this on multiple professional projects though, I really would warn against it. It can produce very hard to find bugs.
Tcl/Tk Used to be the scripting language of choice if you wanted to provide a bunch of C/C++ methods with a (not too complicated) GUI. Python also provides GUI element via PyQT and PyKDE. I think it's much easier to use a scripting language like this to bind to C then trying to do Swing<=>C/C++ bridging.

Multithreading implementation - pro and cons between java and C [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a task to extend an implementation of an ongoing project written in C that is already working well, so that it will be multi-threaded.
I have many years of experience with Java, but very limited for C. So basically I have some options like the following:
Rewrite all of the code to Java and later implement multi-threading in Java
Implement multi-threading in Java by calling some methods from C library through JNI
Learn C and continue the task in C.
By considering that the multi-threaded version does not have to get the latest update from the C library, and probably later I might need to put these code in some big data framework like Hadoop.
In order to find out which option could be better for me, I would need comments about the following questions based on what you have already experienced before.
How smooth it will be to call a C function from Java and if there is any significant limitation?
What are the pro and cons of implementing multi-threading in C?
And also which option that you think is better?
I would go on 3. Multithreading relies on few basic concepts of mutex and semaphores, both in the POSIX standard, and I would recommend you learning them. The C concepts are really basic, the most difficult thing you have to learn will probably be pointers so nothing hard. Having coded multithread projects both in Java and in C I would recommend C (having an hard Java background it's difficult to say for me! ) to learn an important part of POSIX standard (used in Unix too), not to depend on extern libraries and virtual machines. If you want to do it in Java, I would exclude solution 2 and make a standalone Java project, it has appropriate libraries to support multithreading.

Making MATLAB Code Platform Independent [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a MATLAB code (including Simulink models) which I would like to make platform-independent, i.e., allowing them to run on web browsers and smartphone apps. Would coding it in Java be the best solution? And are there programs which can convert MATLAB code and Simulink models into another programming language easily so that I won't have to re-code everything out again? Thanks for your suggestions!
Short answer: You'll have to recode
Would coding it in Java be the best solution?
Probably not. I've found that java is hardly ever the best solution. It may be the easiest, but I doubt it's the best. But for web-browsers, AFAIK, you must render some part down to javascript (even if it's just a shim to fetch data running on a server), flash, silverlight*, or java*. For iphone, you need to do it in C, Objective-C, or C++. I think Android uses some kind of java-like/based language, but I don't know. I doubt Win Mobile 8 even has a JVM, but don't really know there.
* Few people like to leave these plugins open. Too easy to exploit and few sites use them.
And are there programs which can convert MATLAB code and Simulink models into another programming language easily so that I won't have to re-code everything out again?
Mathworks makes an m-code compiler, but you still need a lot of their libraries. It compiles for x86 under Windows and Linux. I think it supports a few other OS's, but all x86 unless they changed it around again. I guess you could try to get MCR working on a phone, but not in a web browser. Realtime Workshop renders simulink models to C, but not if they contain matlab function blocks (or some other blocks, I forget the full list). I hope you have a crap ton of cash, 'cause both those are expensive toolsets.

Is there any difference between Oracle's Java and Java used in Android? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have started to program in Java SE recently, and I noticed from many articles and heard from rumours, that Android applications are developed in Java, or in a language similar to Java.
I noticed also that there are other packages to use, because we have to develop for mobile devices.
Until recently, I was never interested in mobile devices and applications, like the Android OS, but now I understood that this is the future, and a great chance of a job.
My question is: if in the near future, I want to develop something in Android, do I have to learn a different Java language than I used to program now?
Are there a lot of differences, programmatically, between the Java language and Java used in Android?
Thanks.
There is no difference between Java and Oracle's Java . It's called Oracle Java because Oracle owns Java. You can develop the Android application using Core Java.
If you know Core Java then you just need to learn the Android SDK to develop Android applications.
Refer this site for learning Android: Android Developer Site .
Android applications are written (most of the times) in Java. This is a pure java with additional libraries, minus some of the UI libraries (such as swing). It even compiles to class files, and then converted to .dex files which are the executable for the Dalvik VM.
If you already know Java, you'll need to learn the usage of the Android SDK and operating system, not a new programming language.
See some basics at the Android developer site
The omission of many of the base class libraries (eg those that are in the javax namespace) means that while the language is identical, many libraries written in Java will not work out of the box.

Categories