Shortcut to execute .jar in command line (terminal) - java

I have written a program in Java that one can use via the Terminal CLI. I've packaged it in a .jar, and it works nicely, however it happens to be a tool that I'd like to use quite often via CLI.
I am familiar with executing .jar files using:
java -jar MyJarFile.jar
but I'd like to be able to run this using a single command.
The only way I've done something similar to this is by using an NSTask object in Obj-C (my skills in C/C++ are limited), but this is a slightly different situation.
The solution might be staring me in the face, and might be quite simple, but any help to find a different method for launching my .jar would be greatly appreciated.
Thanks

Shell script would work too.
#!/bin/bash
java -jar MyJarFile.jar
save this as RunMyJarFile.sh and give it execute permissions.
./RunMyJarFile.sh
is all you need to do.

Just add an alias to your .bashrc like this:
echo "alias mycmd=\"java -jar MyJarFile.jar\"" >> ~/.bashrc
then source it (or reopen the terminal) with:
source ~/.bashrc
now you can just type mycmd.

you can use Launch4J or other java wrappers.

You've got a couple of choices:
Just click on the Jar from the GUI - on most (all?) platforms, double-clicking on executable Jars will run them. If that works for your use case, that's the easiest thing to do.
Just put your java -jar ... call into a shell script; put it on your PATH if you want. Quick and easy, but it requires keeping track of both the shell script and the Jar file, which might end up being a case of "now you have two problems".
Use a wrapper utility to convert your Jar into a binary or .exe installer. These will bundle your Jar with a script or binary that will unpack and run your Jar automatically for you. It's been a couple years, but I've used NSIS to create an .exe installer pretty painlessly.

Related

Use JAR Files In Bash

I've been looking for an answer, but still a little confused...
In eclipse I just configure the build path of the project and add the jar files that I need, and everything works just fine. When I try to run my programs via Bash, it's not finding my imports.
How do I run my Java programs in bash or on another server with the required jar files?
Thanks
You specify the CLASSPATH. Either through the environment like
export CLASSPATH="a.jar:b.jar"
java com.stackoverflow.Main
or explicitly via the -cp command line option like,
java -cp a.jar:b.jar com.stackoverflow.Main

Double click .jar file to launch it in command line

I'm trying to make one of my command line utilities a little more user friendly. Most of my co-workers don't mind using the utility as a CLI, but navigating to it is a bit of a pain (to them). I'd rather not go to every computer and set up a shortcut in their CLI so:
Is there a way to make a .jar file launch a command line utility into a command prompt (preferably PowerShell?) I tried searching Google and Stack Overflow but am having a hard time making headway... Any direction would be much appreciated.
I somehow imagine this using Desktop, but am not sure how that would work.
Maybe you need to make a swing based console to redirect output and input. Here are the links I found in a simple web search. (I've never used these before)
Message Console
Simple Java Console
And an open source project here at Swing-Console.
EDIT:
Another option. What if you distribute your application with a run-me.bat file?
#echo off
java -jar my-console-app.jar
You can also change the title.
You should reassociate jar files with java.exe instead of javaw.exe. javaw is the non-CLI version of Java, but launching it with java.exe instead should do the trick.
This is how to do that(provided you have administrative rights):
assoc .jar=jarfileterm
ftype jarfileterm="PATH\TO\JRE\bin\java.exe" -jar "%1" %*
Of course, replace PATH\TO\JRE with the correct path.
After doing this, double-clicking a .jar file should open a CLI.
You have at least two options.
Make an executable jar file.
An example on Windows would look something like this:
mkdir temp
cd temp
for %f in (..\dist\lib\*.jar) do #jar xf %f
jar xf ..\dist\YourJar.jar
jar cfe YourJar.jar com.something.Main *.*
You can read more about the jar archive tool here: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
This is a lot easier with Maven. See the Maven shade plug-in here: https://maven.apache.org/plugins/maven-shade-plugin/
Make a Java Web Start application
For an overview, take a look at this:
http://docs.oracle.com/javase/tutorial/deployment/webstart/
For a step by step guide, take a look at this: http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/developersguide/contents.html

Java create executable cmd line program(windows)

I'm new to java and have recently created a stress testing application to test server configurations. Its very simple and all is done within cmd line. I used eclipse to create the jar file and that seems to have worked fine.
The problem that I am running into is making this executable. If I use java -jar in windows cmd line to execute the program, it runs fine. However, I need to be able to run it by "double clicking" the jar file(right now I click on it and nothing happens) or create a .exe which defeats the purpose of java, but this will only be used in windows.
When I click on the jar now nothing happens, but when using the java - jar in cmd it works. Not all of the computers have java in the cmd line, but have it installed. I'm not sure why a cmd window doesnt pop when clicking on the jar?
Again I'm new and any help is much appreciated!!
Create a sortcut icon that will do java -jar yourFile.jar
In windows, you can associate the jar file with the JRE jar runner. Take a look at this post, which explains your options pretty well.
Make a bat file for Windows. You can do this by the following:
#echo off
java -jar YourJarName.jar
Save this in a text file with the .bat extension.
It should run the JAR once double clicked if the JAR file is in the same directory as the .bat file. Otherwise you will have to navigate to the JAR file relative to where the .bat file is located.
You said you didn't want an exe but not sure if this will be ok for you. It shouldn't be a problem for someone to click the .bat file first and will work in all cases under Windows.
Hope this helps.
If you want to get really awesome with it and have it show up in your Task Manager with an app.exe naming and handle any startup options, you should read into JNI. JNI will allow you to wrap the starting and stopping of a Java app using a windows executable and it is actually very simple to implement.
If you want something as simple as a windows exe launcher, there are also tools out there such as Launch4j will create exe wrappers for you.

I need a solution to starting Slick 2D/LWJGL Java project via command line. Help?

Currently I am starting my Slick 2D application via this batch file-
java -Djava.library.path=lib -Xms512m -Xmx512m -jar myapp.jar %1
Where lib is the folder that contains the LWJGL/Slick libraries and myapp.jar is my application.
Although this is easy and effective I want to be able not to have to run a script all the time and actually create a java .jar to do this.
So my question is, how would I 'convert' this batch script into Java code?
Why you would want Java code for this is beyond me as that creates exactly the same problem for you again – namely running your startup Java program that then starts another Java program; you'd be at the same point as before.
However, you don't need to create a JAR in any case. You can stuff all your compiled .class files somewhere and set that as the classpath. A JAR is little more than a main class and a classpath bundled into one.
So instead of your invocation above you can then use
java ... -cp %USERPROFILE%\Java\MyApp myapp.gui.Main
or something like that. Set the classpath with -cp and give the main class on the command line instead of the JAR.
Any -D command line arguments can be set via java.lang.System.setProperty. But the memory parameters can't be set from inside a JVM as far as I know. Therefore, there is no way to do what you want.
Instead you could generate e.g. a Windows executable with JSmooth. Such a wrapper should be able to set all JVM arguments. But in the end the situation is similar to the script. You have some kind of wrapper.
The easiest way is to use a program like JarSplice (http://ninjacave.com/jarsplice)
You can easily create a jar executable with all needed lib. it works very well

how can I create executable file for the program written on Java?

everyone, how can I create executable file for the program written on Java in Eclipse Helios? I mean to create small icon to be able start program only by double-clicking on its icon, thanks in advance
edited
I mean executable for Windows
Export .jar in eclipse. (how to)
Use JSmooth (info) to make an .exe file. (how to)
Here is a tutorial that shows you how to make a jar file from eclipse.
If Java is installed on the computer, you can execute your application by doubleclicking the jar file:
http://ecs.victoria.ac.nz/Courses/COMP205_2009T1/TutorialEclipseExportToJar
You didn't mention what platform you are using. There are 2 ways I can think of.
The easiest way is for you to create a *.bat file (in Windows) that contains the java YourApp command line.
If you want to create a more fancy installer and executable, you can use NSIS script to do so. Since you are using eclipse, consider trying EclipseNSIS to generate the NSIS script, which is much faster and easier than writing it yourself from ground up.
The best answer for this situation is to launch the app. using Java Web Start. JWS can not only create desktop and start menu launch items, but provides automatic updates, cross-platform compatibility and much more.
Create a 1-line metafile to specify which class the JVM should look for to start with the main(String[]) method.
Run the command jar cmf [metafileName] [jarfileName] [classfiles] [img/txtDirectories]
You have an executable jar file - type in "java -jar jarfileName" or, directly "jarfileName" at your prompt. On windows, you can also double click on the jar file logo/name to get it started.
Good wishes, - M.S.
PS: Here is the link to a more detailed tutorial:
http://csdl.ics.hawaii.edu/~johnson/613f99/modules/04/jar-files.html

Categories

Resources