Building file.jar, External Executable File missing - java

Assuming that I use NetBeans 7.3 , I created a project that, in a nutshell, receiving as input a set of parameters, it returns as output a print on screen. The project is made up of a number of directories. Each directory contains a class (in file.class form). One of these directory contains an executable in C. I wrote it as the kernel of the Java project.
I built file.jar and I added it as a library in a new project. When I tried to test it, an error message made ​​me realize that the C written program is not was automatically added to file.jar under construction.
One of my first attempt to solve this problem was to manually add the C-executable file. By using the JAR command from the terminal on my Mac, I was able to update the file.jar adding the executable in the right subfolder.
This solution is not served because, moving from project to file.jar, the relative path that leads to the execution of the C-program has changed. So I tried to change this path seeing it from the point of view of file.jar. Yet this attempt was futile.
I defer to those with more experience than me in the packaging and distribution of Java content.

As far as I know, an operating system cannot directly execute an executable that is inside a zip file (which is what a jar file actually is). It has to be first extracted.
So your program could first open its own jar file and extract the executable file into a file on disk, then run that file.

You can create an installer program, to install both the jar file and the executable file to a suitable location on the user's disk.

Related

Create directories and files by given path with jar

I have a java maven project, which converts .json files to .yaml and vice versa. Long story short,I wrote method which creates resulting directory of converting process using path of where jar actually is. Then I created the method which creates files and writes there result of converting process to the /path_of_resulting_dir/file-name.json/.yaml. In case I run jar from my project in IntelliJ idea - everything is ok. However, when I run jar file from random folder on my desktop, nothing happens.
So, how can I correctly make jar to know if there are any files I should convert placed next to the jar file(e.g. C:\path_to_jar\SomeJarDir\jar.jar So files are in the SomeJarDir with a jar), or if there are any files which I access from path I write as jar argument need to be converted.
Link to the GitHub, where is my code placed(old version where I haven’t tried to make jar to know what to convert from any folder it’s placed, which works only if u run jar in IntelliJ idea):
https://github.com/foreverdumb/javaSprSum2021/tree/JavaSprSmrHmwrk/michaelProject
P.S. How can I force log4j to create log file next to jar?
your program maybe using relative path, so when you run your program from different location, it will read the directory relative to itself. Either provide the path during runtime as user input or set the absolute path to the jar.

Running GUI Application without IDE

Lets say that I built a GUI Application using NetBeans. To run this java application I need to open source code in IDE and then run. I know that I can also run through command prompt.
But how do I start the application independent of IDE. Isn't there some .exe file or something like that, which on double clicking directly runs the application?
If not, how do I generate such a file?
Here you can find how to create .jar in Netbeans: How to create a Jar file in Netbeans
You can run the executable jar on every single computer, on one condition - the system have JRE installed.
If you want to, you can also build the .jar using command line, to do that use the following command:
jar cf jar-file input-file(s)
Description from Oracle doc:
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file. The f
option indicates that you want the output to go to a file rather than
to stdout. jar-file is the name that you want the resulting JAR file
to have. You can use any filename for a JAR file. By convention, JAR
filenames are given a .jar extension, though this is not required. The
input-file(s) argument is a space-separated list of one or more files
that you want to include in your JAR file. The input-file(s) argument
can contain the wildcard * symbol. If any of the "input-files" are
directories, the contents of those directories are added to the JAR
archive recursively. The c and f options can appear in either order,
but there must not be any space between them.
This command will generate a compressed JAR file and place it in the
current directory. The command will also generate a default manifest
file for the JAR archive.
After you build your application look for a folder named "dist" in your project's folder. You should find there a file *.jar which can be run anywhere with double click.
STEPS TO FOLLOW:
create a jar
run the jar

Running an external program from an executable Jar

I have created a simple SWING gui for a cmd program someone else developed. To run this program I execute this line:
Process convertProcess = Runtime.getRuntime().exec("jlyt\\prog\\com_win\\jlyt.bat " + selectedFiles.getName());
The jlyt folder is in the same folder as my src folder (I am using IntelliJ).
When running via IDE everything works great, but not when I run the jar I created. I have tried running it from the directory it was saved to by IntelliJ as well as from the directory of the jlyt folder.
I did not add the external program (inside the jlyt folder) to my jar since it is very heavy. I want my jar to be distributed along side the original program and not to contain everything.
Any idea how I should build my jar?
Thanks.
I see why you put /jlyt in /src, it serves as a convenience within the IDE. /jlyt will get copied as a resource into /bin/classes, or whatever the IDE target is, and that allows everything to work from the IDE.
When you JAR your application, /jlyt is typically added to the JAR; however, it is not accessible to Windows. I assume you are placing a copy of /jlyt in the same folder as the JAR when you attempt to run.
As a first step, in a terminal, set the current directory to the folder containing the JAR and /jlyt. Since you are specifying a relative path in your exec(), that should be sufficient for everything to run.
You can also try creating a shortcut to the JAR, since it is executable, and set the working directory to the folder containing the JAR.
You have to use a relative path based on the place where jlyt.bat is according to your jar file.
e.g. use "./" or "../" to navigate the directory tree up.
The location of the JAR file is only relevant for starting the JAR. The working directory must be the directory that contains the jlyt folder, since you are using 'jlyt\...' as path to the executable.
Example, lets suppose following directory structure:
somewhere
project
gui
appl.jar
jlyt
prog
...
working directory must be 'project' and the JAR will then be referenced as 'gui\appl.jar
C:\somewhere> cd project
C:\somewhere\project> java -jar gui\appl.jar
Also be sure to wait for the convert process to terminate (e.g. convertProcess.waitFor()) before exiting your application/java - I believe that any running external process is killed when the Java Virtual Machine is closed!
Hint in documentation of Process:
As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.

How to make jar files that draw from a source folder

I've been wanting to make executable jar files with java lately. When executing my code with Eclipse it works perfectly. But when I use Eclipse to export the same code as a runnable jar, Most of my jars work except the ones that draw from separate source folders.
The jar will be made but when launched it will try and open and then just say to check to console for possible errors. I try and run the jar through the console with the command "java -jar test.jar". and It says it cannot access the jar. Any Ideas? Btw Im on a macbook pro osX. Thank you!!
picture of where my files are within eclipse
If you have a file you want to store in a jar and access from there, you don't really have a Java File any more. Look at Class.getResourceAsStream() and Class.getResource() - the first can give you an InputStream to the (used-to-be) file, the second returns a URL and can be used for things like images. Note that the file being accessed can be accessed relative to the package/folder location of the class or relative to a classpath root (by putting "/" at the front of the resource name ("/resource/funny.jpg")).
When you execute the jar from a command line, be aware that you have a thing called the "default directory"; it is a folder in which your commands execute by default. If your jar is not in the default directory, you have to specify a valid folder path to your jar to execute it.

Jar file execution through use of BAT file

I have created a simple class with only inherent Java dependencies (java.io, etc).
I have set up my jar file and the bat file in the same folder. The BAT simply reads:
java -jar "MyApp.jar"
pause
I have been able to run it from several different locations on my computer. But when I sent it to a coworker as a zip file, he was unable to run it by double clicking the BAT file.
The command window came back with an error
could not find the main class: MyApp.MyApp. Program will exit.
I've poked around this site but most similar errors involve use on the same computer.
Yes the other computer has Java installed 6.29
Any help much appreciated.
Two options that I can think off the top of my head:
1) He might not have extracted them both to the same directory (or) after extraction, he might have moved around the JAR file to another location.
2) His classpath does not include the current directory. Your classpath has a '.' (indicating the current directory) while his doesn't. If that is the case, you can probably modify your command to include the '-cp' switch.
In order to run a jar that way, you need a META-INF folder inside it with a manifest file inside that. The manifest file needs a main-class line that points at your class with a main(). Your IDE probably added that, but maybe in the process of extracting things he unzipped the jar file too, or something "interesting" like that.
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Categories

Resources