How to put java code into an application format? - java

I made a simple command-line based game in java, only two classes (using Eclipse). But I was wondering how I can make this into a usable application for anyone, without running it through eclipse (ie send it to someone who knows nothing about java but would still be able to play the game)? Thanks!

You want to create a runnable jar file.
Eclipse has an option for this in the "Export" menu. For more options, search for "executable jar file" here or on Google.
You want to make sure that you also include any jar files your code depends on as well (Eclipse can also do that for you).
Users will be able to start this by double-clicking on the file on most platforms. If you need better integration (such as a custom icon), you will need to bundle it up further into an OS-specific executable. But for starters, a simple runnable jar works fine.
send it to someone who knows nothing about java
You need to get them to at least install the Java runtime on their machine (if it is not already there).

Just to be clear, "command-line" and "knows nothing about java" are probably not going to work very well for you given that:
java is OS agnostic, therefore, if you send (presumably) a jar file to say...your grandma and she has a mac and you have a PC chances are her getting it to work is not going to be "out of the box easy" so to speak.
Left with this, I think you have a couple choices...first off, you do need to package your classes - a runnable jar will work fine. Aside from that, you will most likely have to build OS specific scripts (batch scripts for Windows, shell scripts for unix, etc.) and you will have to hand these out with your jar file. That being said, the intended user will still need to have java installed, and the batch scripts themselves are not likely to be trivial endeavors.
Your next option would be to use JNLP. However, I don't think JNLP has a command line mode, so you will likely have to simulate a console with something like a JTextArea.
As far as I see it, your last option it to use one of the many products (not sure if there are any free ones) that package java into native code. I think Exe4j is one such example - but, like I said, I am not sure if there are any free ones and I am not sure how hard they are to use.
Best of luck, and if you can't get your jar to work you should probably move that to its own question.

Related

Is it possible to embed JDK and other requirements into runnable jar?

Two days ago I got close to finishing a java program that wrote, I wanted to let some friends play with it for a bit to find flaws etc. I created a runnable jar trough Eclipse and then I used Launch4j to transform the runnable jar into a .exe
It worked perfectly fine on my pc, but any other user couldn't open it. After a while I discovered that when people would install java JDK, it worked for about 40%, but this atleast pushed me in the direction of what the flaw is. Is there a way to (automaticly?) embed everything in the jar that my program is in need of? Or is there a way to determine what exactly it all is that people need for the .exe to run and what they are missing by running some pre-checks that can re-direct them to links where they can download this? I can't seem to find much on this subject, so I probably made a mis assumption somewhere, any help/clearance is appreciated!
There is a new tool called jpackage, which should do what you want.
Also see the User's Guide.

Export a JavaFX project in eclipse to a runable jar

I need to send a project made by JavaFX SDK 16 and Java SE-15 using Eclipse to my friends, but when I try to export it, i'm getting surprised that Eclipse doesn't insert the VM arguments to the exported file :
and as you know since JavaFX SDK 10, JavaFX libraries are not anymore part of the Java SE, so we need to tell Eclipse where to find them by VM arguments, in short words, these VM arguments are really important to run my program.
I already know that I can insert those arguments while using prompt command to open it, but is there a more simple way? just by clicking on the exported file ?
Your main problem is that you are trying to do something that does not make any sense. Runnable jars are not a proper distribution format for JavaFX applications for various technical reasons. The way to go is having a look at jpackage. That's the proper way of dealing with distribution nowadays. Otherwise your are just banging your head against a wall.
Here is a good summary of all the options you have: https://stackoverflow.com/a/68823040/4262407
I have this problem 5 years ago (I use netbeans IDE, so I don't know if it works in eclipse, but I think yes), and I cant find a good way to do this.
Instead, I created one Swing application, and put all code of JavaFX inside.
You could see how I do in
this link from Oracle.

How do i get the working directory of a windows process in java?

I would like to get the path to the working directory of a specific process (for example for the PID of the process). I am Not Talking about the working or current Directory of the process where my Java Code is running. Its a simple task with Linux, but for Windows i cant find a proper solution. Furthermore, it would be nice, if its a Command or a Framework for Java, because i will need the path in my Code. I am not looking for the path to the executable, also Not for a solution with wmic or process explorer.
Already thanks for the help.
I already tried commands like tlist and wmic, but those solutions cant be utilize in my code. I am looking for a solution that i can use without special installations on Windows.
JNI and JNA provide means to call directly into native libraries from Java code, and it is feasible to use these to call out to Windows libraries.
There is a github project that appears to be close to the need: https://github.com/kohsuke/winp. Perhaps you can add the needed code and send up a pull request, or fork the project.
Note that any solution here is going to be windows-specific, meaning the application using it will not run on another platform. Given the nature of the question, that doesn't sound like it would ever be a concern.

How do I make a java .exe to allow user to interact as I would in the eclipse command window?

I wrote a simple java code that would take simple inputs from the user in the command window (of eclipse for me) using nextInt() and nextLine(). However, I realized that others need JRE (I believe?) on their computer to run the executable jar file made. So I was wondering if there is a way to get around that by making the app produce a window that is like the command window to have the same interaction as the command window in eclipse.
So, if I were to run the .jar or .exe then a simple window would pop up that acts like the console of eclipse displaying lines from System.out.println() and etc.
To run a java program you need the jre. There is no way around that.
If you need the console, nothing is stopping you from running the java program from the windows command line, which will do exactly what you ask for.
You still need the JRE.
Unfortunately, when starting to learn Java with Eclipse, many people miss the opportunity to at least start to understand how to do the same from the command line, which is, if you ask me, good to know.
For programs written in Java, they are compiled as a jar file, like you mentioned, and how these compiled versions of your source code differs from many other programming languages is that they do not contain the assembly/machine code like for example a compiled C program would have. They are instead compiled as bytecode. Which is special code for execution by a Java Virtual Machine. Here is a good Wikipedia reference: link
To answer your question, yes, others need a JRE (Java Runtime Environment) and this can be either:
Installed by themselves (this is what you mentioned)
Packaged together with your java app, to provide a download-and-click experience.
For option 1, assuming they already have it installed, they can simply run it by executing the jar file with javaw, more information on that is in this previously answered SO question
For option 2, the process is fairly lengthy and I'll point you to the official docs to refer to: self contained executables and Deploying java apps
If you have a more complex project with third party libraries and what not, look at this SO question
In the past, I've also found launch4j, a cross-platform wrapper to be very useful, it automates the process of going from jar to an executable (made a simple game that using Swing, simple and ugly thing it was), but the user still needs a JRE, nonetheless. :)
Hope this helps!

java: how to copy the currently executing file

I have a jar. I want the jar to be able to make a copy of itself while running. I understand windows may have problems with this. How would I do this, or am I over thinking it?
Edit: To explain a bit more....
I'm writing a repackagable firmware deployment system... http://code.google.com/p/heimdall-one-click/ The idea is that a ROM developer from XDA can make his own, then pack it up in a cross-platform deployable one-click packaging nearly as easily as it is to deploy the firmware.
My program takes alot of the work out by automating the tasks... I'm trying to automate packaging of the one-click deployable packaging system.... give the developers a form to fill out which will change the header information, then they select their firmware files to be deployed. I'm trying to keep it all in one jar.
As josh says, it would be nice if you tell why do you want to do this in order to help.
Answering only what have you post, copying the jar is just copying another file. There is the issue that it might be blocked by the OS (Windows); another issue is how do you locate it in the machine and if the user running the process has the permissions needed.
Once those two issues are solved, it is just a copy operation, the OS could not care less that if the order to copy comes from the process run from the file or from another one.
EDIT to asnwer changes in the first post.
As I told before, in the end copying a file is a OS issue. If you want to copy the current jar in Windows, then the jar must not be locked by other process so it becomes an OS question rather than a Java one.
Possible workarounds:
The faster (but dirtiest) is to launch a .bat that does a sleep of a few seconds and then does the copy. Immediately after launching it, your close your java app. If you need to continue doing things in Java, after copying the file, the .bat launches the java app again (with the appropiate parameters).
A variant of the previous is slightly sleazier... launch your java app from a .bat, and the first thing that .bat does is copying your jar to the PC temp directory. Be sure to document it well so your users do not get scared!
JNI library to unlock a file. There are several programs that (Unlocker) that try to unlock files; do not know to which point it is effective or how will it affect the JVM.
I believe you can use:
File file = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
to get a reference to the path to the .JAR file.
Then you just make a copy of it:
http://download.oracle.com/javase/tutorial/essential/io/copy.html
Your operating system might not allow this, but I think it should.

Categories