I'm aware of all the other questions about this topic, but I haven't found a good solution for my problem. Currently I am trying to use Java code in my C# project. I've already tried to convert the JAR files into .DLL files with IKVM, but this didn't work for me because the JAR files are to complicated to translate into C# because not every component, which were used in Java, can be found in C#. The normal call of the JAR file doesn't work for me either because I need to work with the class instances of the declared classes in the Java code.
Back to my question: Does anybody know how I could use Java code in my project? I've heard that it may be possible to implement Java code like it would be native, is that true? Note that I've to work with the class instances of the classes declared in the Java code.
I highly appreciate any kind of help, sheers!
Edit:
My work around would be that I include batch files, which are calling the JAR files. I will include these batch files into my C# Project and work with the batch files. This may be a even better approach for me because every input and output of the JAR files are done via XML files.
This kind of mixup are not generally a good approach. I think the .Net Framework is very mature and you can find everything you want to do your work.
I would suggest you the following approach :
You can wrap your Java library in a REST API and call it in your C# code. Your REST API can be hosted on an external server or use an embedded server or even a spring boot project.
You can read this post for more details.
Related
I am trying to write a standalone Java application in IntelliJ using edu.stanford.nlp.trees.GrammaticalStructure. Therefore, I have imported the module:
import edu.stanford.nlp.trees.GrammaticalStructure;
Currently, Intellij doesn't recognize this and many others of the imported external libraries (cannot resolve the symbols) and is also not able to automatically download/import them.
Is there a way to use the GrammaticalStructure class without having to download the entire Stanford CoreNLP .jar and adding it to the project as a library? This question applies to other dependencies as well, since I want to use other external libraries but avoid including their .jar files as much as possible (to minimize the size of the final application, given that it will be standalone). Unfortunately, all the solutions I have found proposed exactly that.
Apologies if I have overlooked some basic setting or setup steps, it has been a while since I have worked with Java.
Any help is greatly appreciated.
If you want to use it means you want to execute the code in them. How is the runtime supposed to execute code that is does not have? How is the compiler supposed to know how the code is defined (e.g. what the classes look like)? This is simply impossible. If you want to use the code you have to provide it to the compiler as well as the runtime.
If you just dont want to include all of that code into your application, you need either access to the sources and just pick the class you need or you need some kind of JAR minimizer as #CrazyCoder suggested.
I'm trying to read a .qm translation files with Java.
.qm files are binary files. I don't have access to the .ts files.
And I don't find much info on these .qm files.
How are they structured ?
Regards,
There's no documentation that I know of, but if you look at QTranslator::load you should be able to follow the format of the QM file.
You will probably need to reimplement QTranslator in Java, as you need not only the ability to load the files, but also to extract and apply translations in Qt fashion.
As per request of OP:
You could use those files by using the Qt libraries and JNI. By using the translator in a c++ dll you can translate strings easily. However, you cannot extract the files or list the contained translations. But if all you need is the actual translation, this solution should work.
I cannot give a real example, because I only now how it works in theory, I haven't tried it, because it's not trivial. But if you are eager to try it out, the general idea would be:
Create a C++ dll and build it against QtCore. The easiest way is to download Qt from their website qt.io. You can for example create a default library project with QtCreator. Note: Besides Qt5Core.dll, Qt requires other libraries to correctly run. They are all included in the installation, but once you deploy your application, those of course have to be includes as well.
Include JNI to the C++ project and link against it. if you're new to this, here is a nice tutorial: Java Programming Tutorial
Create your wrapper methods. Methods in cpp you can call from java that take java strings, convert them to QString, translate them with QTranslator and convert them back.
Load the library in Java and execute those methods
Important:
First, I don't know how java handles dll dependencies. If you encounter errors while loading the dll, it's probably because dependencies of your dll are not present. Second, Qt typically requires a QCoreApplication running in the main thread for most of it's operations. I tested the translator without such an app, and it worked. So apparently for translations only the app is not required. However, depending on what you do in your dll, I think this is important to know.
If you need more details, feel free to ask.
I know there is a lot difference between PHP and Java. My requirement is to package the PHP code so that it can be distributed to the customers.
I am planning a PHP application which can be packaged to the customers and can be installed at their end. I am looking for a possibility to hide my source code from the customer. Like JAR file in Java. I know we can have PHAR file, but that again doesn't solve the complete problem. You cannot package very big application into a PHAR file.
While using PHAR file, the only solution is to package small libraries and keep rest things intact.
Is there any other way to acheive this use case?
A good alternative will be ionCube, its not a packer but it can encode your Source and hide it in this way.
ionCube
I know how to create a jar file using Eclipse.
I was trying to create a share library so that I can avoid redundant source code. I have figured out that a jar should be :-
independent
should not make call to external class attributes(properties)/methods except the standard library imports.
The resources should be given as a parameter to jar file to perform a action.
Should work as a independent entity.
I tried to well organised my code in different packages also added MANIFEST.MF file.
This is first time I'm trying for data abstraction.
I would like to request suggestions/instructions as per the programmer point of view, what are the criteria that jar code should have ?
Is it good idea that my jar is or depend on another jar (viz java mail api jar) ?
Thanks in advance.
As you've tagged this with Android, I assume that Android is the intended use case.
The easiest way to share your code between several projects is probably to create a library project, this way you can keep the source code at hand too (less convenient to attach source to the jar every time you use it).
Javascript is executed by Java application. However, something like Jquery library is really too long to fit into a String variable. I am able to read jquery.js from a file but not sure how to package it inside the .jar file.
Loading the .js files is the same as loading any other resource from a jar file. Generally, this is what I do:
For files stored in the root of the jar file:
SomeClass.getClass().getClassLoader.getResourceAsStream( "myFile.js" );
For files stored along side a .class file in the jar:
SomeClass.getClass().getResourceAsStream( "myFile.js" )
Both techniques give you an InputStream. This can be turned into a String with code a little bit more work. See Read/convert an InputStream to a String.
This technique is for when your resource files are in the same jar as your java class files.
There are all sorts of places you can keep your JavaScript sources:
In the CLASSPATH. You fetch them with getResourceAsStream()
In the database. Yes, the database. You fetch them like you'd fetch any other CLOB.
Personally I've use both approaches for different purposes. You can keep your JavaScript files around in your build tree in a way that exactly parallels the way you keep .properties files. Personally I just keep them in with the .java files and then have a build rule to make sure they end up in the .war, but they can really live anywhere your build engine can find them.
The database is a nice place to keep scripts because it makes it much easier for your web application to support a "script portal" that allows dynamic updates. That's an extremely powerful facility to have, especially if you craft the web application so that Javascript modules control some of the more important business logic, because you can deploy updates more-or-less "live" without anything like a deployment operation.
One thing that helps a lot is to create some utility code to "wrap" whatever access path you're using to Javascript (that is, either the Sun "javax.script" stuff, or else the Rhino bindings; at this point in time, personally I'd go with straight Rhino because it really doesn't make much difference one way or the other anyway, and the Sun stuff is stuck with a fairly old and buggy Rhino version that in the current climate will probably not see an update for a while). With a utility wrapper, one of the most important things to do is make it possible for your JavaScript code (wherever it comes from) to import other JavaScript files from your server infrastructure. That way you can develop JavaScript tool libraries (or, of course, adapt open-source libraries) and have your business logic scripts import and use them.