Run jar in a java apllication ( pdfbox ) - java

I'm programming an application in java that stores data and images to a pdf. I'm using pdfbox. I saw that pdf box has some jar like the one for data encryption that has to be run by command lines.
Is there a way to run those jar in one of the functions of my class? I mean start the encryption processed by this jar after an event. How do i launch command lines from within my class?
For example:
if(IHaveData() && FileCreated())
*Encrypt the data*
Thanks in advance to everyone who wants to help even if the question may be stupid or something

We do not run jars. We run applications. Java applications consist of a collection of classes and resources that can be stored either in file system or in jar files.
To run java application you should use command line like
java -cp one.jar;two.jar;three.jar Main
where
x.jar - the jar files
; - separator for windows. Use : for unix
Main is a fully qualified class name of your entry point.
You obviously can run any command line from java program using Runtime.getRuntime().exec() or using ProcessBuilder. "Any" means that you obviously can run java application from other java application.
However this way has a lot of obvious disadvantages. Instead you should run API that is exposed by library you are using. If it does not expose convenient API you can in worse case directly call the main method from your program:
Main.main(new String[] {param1, param2})

Related

Java calling an external process - exe

I am building a java application, which at some point utilizes an external exe. At this point, I'm trying to simply add this exe as some sort of library, which I can use in process call, so user wouldn't have to install it..
This exe file is an command line tool which produces some output, which is further processsed by the application.
So my question is, how does one include exe file within a java application, instead of calling it as a system process. Also acceptable would be, if this exe would be for example in the final lib folder, where java app would fetch it and execute it.
I hope it is clear and thanks for any help.
Java interacts with other (native) code in a couple of ways:
to specifically coded libraries through Java Native Interface (JNI)
to external tools by forking a child process using Runtime.getRuntime().exec(...)
through network communications
In the first case, it is useful to include your JNI library with you Java program. It's thinkable to include it as a class resource in your JAR file, but that would be against the ideas of JAR files (holding classes and resources). More likely you should bundle the library (for all target platforms) with some sort of installation package (e.g. InstallAnywhere or others)
In the second case it's not useful to include the binary into your Java program (i.e. in the JAR file). Rather it most likely has (should have) its own installation procedure and your Java code should use an appropriate commandline (PATH) to find the executable.
I think the third case is not relevant.

How can I create a command line application in Java? [duplicate]

This question already has answers here:
How to create a Java application which can be run by a click?
(8 answers)
Closed 9 years ago.
I am in the process of writing a command line tool which should work on all popular platforms (Windows, Mac & Linux). I was thinking of writing it in golang because it can generate statically compiled binaries and works on all the above said platforms. Before I take the decision, I need to evaluate Java and see if it can do this.
I'm wondering is it possible to create first class command line programs in Java? AFAIK, a Java program needs to be started like
java -cp classpath MyApp
Now I need to wrap this in a shell script to improve the experience. Something like,
#!/bin/sh
java -cp classpath MyApp $#
Now user can do:
myapp --arg1 value --arg2 value
The problem is this approach is not cross-platform. On Windows, I need to write a batch file to do this.
I'm not sure if this is the right way to do in Java. Is there a better way to handle this problem?
the solution you posted (platform specific script files invoking java) is indeed the easiest and quickest to implement. if thats not enough for your needs there are several solutions which provide better usability:
executable jar files. basically by stating your main class in a MANIFEST.MF entry the file becomes executable by double clicking on some platforms (windows definitely, the rest im not certain). note that this requires a properly installed JRE on the machine.
service wrappers. there are 3rd party products that will wrap your java application in a native executable and allow your application to "feel" like a native app. the most commonly used one is the tanuki service wrapper but there are others (launch4j is an open source alternative that fits your set of required operating systems)
EDIT - more options in a related SO question here
To work with command line arguments comfortably, you can use Apache CLI. It parses arguments and print a list of them if some are missing. In java projects it's always OK to have *.bat and *.sh files both in the bin folder. What confuses you in this approach? Have a look at Ant or Maven projects for example.
I would probably suggest to use Maven, It has a concept called "maven spring shell" which would be easy to develop. You indeed need to write .bat and .sh for windows and linux, but it is more of a configuration type. Would be easier to package.
The following link would help you developing spring shell applications

Converting dll to jar

I'm trying to find a way to convert a dll to a jar file. I have a .net application that communicates with a java application. The core entities are .net objects which I have to duplicate manually in java.
I've read about IKVM but it seems that it converts only jars to dlls and not the other way around.
Edit: If there is a tool that creates java classes from a dll it is also fine.
Thanks in advance
There isn't such a tool.
A dll is a natively compiled library. That means its been compiled down to machine code. Probably compiled by a C/C++/C# compiler.
A jar file is a zip file that contains '.class' files, which are files compiled down to 'java virtual machine code'. Probably compiled by a java/clojure/scala compiler.
These are two very different incompatible things.
It's not impossible to create such a tool that would do this translation, but it would definitely be an extremely difficult task, as it would entail translating from one machine code, to another, and would need to manage multiple issues like dependency solving, different type structure etc.
HOWEVER, I'm imagining that you want to do this because you want to use a DLL within some java code. That is somewhat possible, but is actually quite complicated. You'll need to use the JNI.
Take a look at this question as it might help you achieve what you want to do:
Calling C++ dll from Java
This is actually an easy task to perform. Converting .dll to .jar is as simple as using com4j and a couple of commands on the command line.
Download com4j.
Open command line and navigate to com4j directory in above step.
Execute below command.
java -jar tlbimp.jar -o outputFolder -p nameOfPackage "pathToFile"
Then jar the results with the following:
jar cf desiredJarName.jar folderYouWantJard

Questions about .jar files and running a java program through PHP

I'm building a website which will use files in order to render social graphs. These files are created by a backend in JAVA. This JAVA program consists of 4 classes and 4 libraries. I know that in order to run a JAVA program through PHP, I need to call it with the "exec" command but all the examples I saw have .jar executables and not .java files in the syntax. So, I'd like to ask two questions:
Is it possible to call just the .java main class from PHP without creating a .jar file? And if so, what's the syntax of the "exec" command?
If I have to create the .jar executable, would I just have to place it in the website folder and just call it? I mean, does the .jar file contain all the classes and libraries that the program needs? I don't talk about JAVA libraries, I have 4 specific libraries (MongoDB, Neo4j, GEXF parser and Lucene DB) which are not part of Java defaults. Thanks in advance and please forgive my syntax and spelling mistakes.
You have to at a minimum compile the .java files into .class files. It would be easier if they are in a jar file, but that is not required.
1)
a) java -cp /location/to/.class/files
b) java -jar /location/to/the.jar
2) the jar file does not have to be executable, though if that makes it easier, set the manifest up correctly and there you go.
When you say libraries, you mean other .jar files or native .dll or .so libraries? If the later, you have to have your library path setup to find them.
There is a PHP-Java bridge project on SourceForge. Also, you could expose your back end process by running it as a servlet, which is probably the proper way of doing it. If all else fails you can just "exec" it.

build multiple shared libraries

Hi I have a java program which has to invoke a native program, and this native program are given by two so files. So I create my so file in order to use this native program APIs to do something for my java program. I was trying to merge two so files with my created so file into single one, and run my java program. However, it seems that it failed this way. To be more concrete, here is my example.
I have a java program A which has to invoke some native code. Therefore I've written some native code and built it as a shared library (called: C.so).
Unfortunately, the native code I've written have to use other code which is in other so files. (A.so, B.so)
Thus, any ideas how to compile my so file with A.so and B.so in order to make my java program work?
I'm assuming the following:
When you link c.so, you are listing a.so and b.so on the command line.
When you run ldd on c.so, you see a.so and b.so.
When you run, you set -Djava.library.path to include the directory containing all three.
When you run, you do NOT set LD_LIBRARY_PATH to include the directory containing all three.
You will get the desired results if you set the LD_LIBRARY_PATH environment variable to include the directory with the libraries in it.
For more explanation, and an alternative, see https://github.com/bimargulies/jni-origin-testbed.

Categories

Resources