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 2 years ago.
Improve this question
I'm currently on a school project, we made a FuzzyLogicFramework in C++.
Now, I try to use this framework in Java in order to do an android app using the framework, I have a factory called FuzzyFactory in the c++ project.
the prototype for its methods are like this :
Expression<T>* newAnd(Expression<T>*, Expression<T>*);
I don't necesserly want to use all the avaiable types that is why I created a class like this :
template<> class FuzzyFactory<float>;
I generated a DLL library with this class.
My questions are the following:
I want to use this library in a Java file : what technology could I use?
I heard about JNI and was able to run basic functions with it, but would I be able to manage objects instanciation ? How would I be able to give pointers in my functions arguments in Java ? I was able to run my c++ project main in Java using JNI, but it seems using a Factory class with it is on another level.
I also heard about wrappers and JNA
I just need somebody to told me a technology to use, It's my first time trying to do cross language implementation so I'm a bit overwhelmed.
if you want to see a bit more about the current state of the project : https://gitlab.com/MelvinC/languageframeworkproject-ensisa-2020
Should my Java project and c++ project be in the same git repository ?
Sorry for my english, if it is not good enough.
Thank you in advance for reading and helping me.
SWIG has some support for C++ templates and can generate Java binding code from C++ headers. That will give you a very low-level, but hopefully usable interface.
Alternatively, you might want to consider some sort of external representation of your logic program. You can then use plain Java code to construct the program, then serialize it into the external representation and pass it to the C++ side.
For inspiration, the Z3 SMT solver accepts a representation called SMT-LIB.
Alternatively, you can create your own format using S-expressions, XML, or even just nested JSON.
Related
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 4 years ago.
Improve this question
I want to call a java file(java class) from index.html using href without using java-script in-between html and java.
I am using springboot and my java file is inside src/main/java/controller and the html is inside src/main/resources/templates.
So anyone please help me to find whether it is possible or not,if yes then how can we achieve that.
Thanks in Advance..
Updated from OP comment:
Thank you very much for your reply and let me correct the question can
I call a function which is written in java directly from a html. –
JAVA Coder Nov 5 at 9:08
Still the same answer. Methods or functions, terminology aside. What you're proposing is like trying to power a bicycle by attaching an engine piston to it without the engine. Does not compute.
You cannot directly "call" a java process from html/js.
---- END UPDATE
No.
Longer answer:
Java files aren't called. Java classes are. The Java runtime has to live somewhere for java to be used. Typically that would be in your app server/web server. Its entirely possible to use java to generate the html, but the way your using the term "call" in this case doesn't make sense. Html doesn't "call" anything as its really just a rich text implementation (like a painting). Modern browsers implement javascript interpreters (which has nothing to do the Java) to run javascript code.
So, can you write html to "call" java (without javascript): No. Can you use java to generate html: Yes. Can you "call" java from javascript. Only if its are exposed as a web service (e.g. classes in the app server are configured to present http content)
A programming class and web services overview would probably be helpful.
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 6 years ago.
Improve this question
Does Java use libraries to determine which commands perform specific actions?
After learning Java for a few weeks now I don't really understand how Java recognises which commands perform specific functions in code.
For example, how does Java know what to do when you use a "do while" loop? And how does Java remember so many different commands, is there some sort of master list or a combination of libraries that document recognised Java commands and their functions?
This may seem like a real noob question, but it's been bugging me for a while and is getting in the way of my understanding of how code, and specifically Java works. Thank you all in advance.
Edit: Just to make this more specific, I was confused because I didn't know how new functionality is added. So, as I understand, new commands and functionality is added to Java using additional packages alongside the JDK, which contains a list of the most fundamental Java commands?
The commands and the full syntax of the language is defined in the Java Language Specification. The Java compiler generates low level instructions (i.e. bytecode) according to that syntax for language constructs like the do-while loop. The JVM is then responsible for executing those bytecodes.
I understood your questions in two ways:
1 - Where does java hold its lots of functions, classes, etc.
2 - How does java work (how does an if/else work) under the code's skin.
First, there is no noob questions, we are all here to learn :)
Second, about the questions:
1 - Java holds its classes in the Java Development Kit (JDK). Because of this, when you create a Java application, you need it installed in your machine, so Java will find a lot of its classes in the installed JDK.
You can expand the amount of classes by adding new packages (jars) to your project. Then, Java will see all the JDK classes and your added jars.
2 - If you mean to "how does java work in a do-while loop or something related, you want to learn about javac, java's compiler.
Javac will get your code and transform into an jar file. The Java Virtual Machine interprets the compiled code to do memory operations.
For exanple, when you assign a variable to java, internally you create a space in memory with the datatype's size.
When you do a while loop, the JVM will use other VM functions to do the job.
That's actually a quite advanced question. I think you can find more about what you want here.
Hope it helps! :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm actually using OpenCV for face detection, but after watching this video : https://www.youtube.com/watch?v=LsK0hzcEyHI , I've noticed that dlib is much more accurate, and even in my tests, gives a lot of fale positives (but doesn't miss any face), does anyone know how to use dlib within a Java web application (not android) ? I've already found a port to Android, but I don't thinkk it's possible to use it with in a java web application.
Thank you
I have used dlib myself, and yes it is a very advanced and precise library. There is no port in Java as far as I know. But you can always have it compiled to a shared library (.so in linux or .dll in Windows), and then use that inside your Java code.
More on using a dll or using an so in java
There is also an option to use the C++ library directly within Java, using proxies. You might want to look into that.
Edit : As per #evgeniy's comment, most of dlib is header-only templates. You will not be able to use those if you export dlib directly as a shared library. What you might want to do instead is to expose whatever APIs you need, see here
Edit 2 : As #davis-king's pointed out, you may want to look into using swig and cmake as is done in the mitie/dlib Java api : See here.
If you are looking for Android: https://github.com/tzutalin/dlib-android
Otherwise, this https://github.com/bytedeco/javacpp-presets/issues/49 looks like the most promising but still opened at the moment.
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 9 years ago.
Improve this question
How can I publish an API with Java? In languages like C or C++ it is really quite easy because you can simply divide headers from code, but in Java this is a complete different story. So I know that there is no real way in Java you can obfuscate your code, even if you "obsfuscate" it, because it can be easily decompiled and analyzed. But if I don't simply can distribute headers to someone, what is the preferred way to publish a API in Java? I don't have special needs because I am in the beginning of the designing process so I am really dynamic and I would like to know all alternatives I have.
A clean way is to define your API purely in Java interfaces, include those into a separate API module and make implementation module depend on the API module. This does not provide the same functionality as separating C++ header files, but it is a good idea to program to interfaces anyway completely separating those from a particular implementation.
You don't need to publish your API as header files. Everything the developer needs is already in the JAR. If you want to publish documentation publish the java docs of the code.
You can obfuscate your code using a professional java obfuscator. Then it is not easily decompiled and readable. You can then publish your jars and javadocs like others have mentioned.
You could split your library into multiple jars and provide one with the classes and interfaces that form the api and another one that contains the implementations of those interfaces.
However, note that the hastle might not be worth it. Why exactly would you try and obfuscate code the users of that api would need anyways? What I mean is, that whoever would use your api would also need the implementations of the interfaces to run the application, so they'd still be able to decompile your code.
Generating an api-only jar would help with separating api and implementation though (which means you could replace the implementation or prevent accidential direct access to the implementation).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I am developing for Android under Eclipse. I have some c++ (.h and .cpp) files that I wish to use in my Android application. I have read Programmming in C/C++ with the Java Native Interface and now I know how to make .so file and import it to Android. But the main problem is that:
Is there any tool that converts c++ (.h and .cpp) file to java style files which I can compile and create .so library ? I have heard about JNI - javah and javac can that tools help me and if they can so HOW ?
Edited:
javah is a useful tool that creates a C-style header file from a given class. The resulting header file describes the class file in C terms. Although it is possible to manually create the header file, this is almost always a bad idea. javah knows exactly how Java types and objects map into C types. For example, an int in Java maps to a long in C, and a long in Java maps to a 64-bit value, _int64, in native code. How to use this javah?
Anybody have expiarance of how to use functions that are implemented in .so library in android application ? If anybody have code examples or useful articles links please give.
Thanks in advance !
You don't "convert C++ file to java style". You need to create a JNI wrapper around your existing C++ code. This JNI wrapper is actually C++ code that can be called by Java.
By wrapper I mean that you shouldn't have to modify your existing C++ code base. This wrapper, or better said this binding should generally be very thin. The wrapper code is only meant to expose existing functionalities, not to implement them. It is better to leave the implementation in the (portable) C++ code base.
If the code base isn't too large, then I recommend that you write this wrapper by hand, as explained in The JavaTM Native
Interface
Programmer's Guide and Specification
Now, if you are trying to bind a large library, it may be problematic. So, in regard to tools, I haven't used that, but have a look at SWIG, and the relevant SWIG Java documentation.
According to the homepage description, it's what you're asking for:
SWIG is typically used to parse C/C++
interfaces and generate the 'glue
code' required for [Java, Python, PHP,
...] to call into the C/C++ code.
javah can be useful in certain cases, but it's not what you ask for. It extracts JNI boiler plate code out of native declarations found in Java classes. Regarding javac, it's the Java compiler, that's irrelevant.
When developing for Android you are not limited to using the Java language. Why not use C++ directly? See e.g. the Android NDK.