Drop file onto .jar and pass filename to main() - java

I'd like to drop a file onto my jar and have the filename passed on to my main method as argument.
I'm on OSX.
How can I do that?

If the app. is deployed using Java Web Start, a file association can be declared. Double click any file of that type and the path of the file is supplied to the main(String[]).
BTW - here is a demo. of the JNLP file API. Note well the 2nd bug report, which would mean a minimum version of 1.6.0_10+ in order not to delete a file dropped on the application's icon.

To do this on OSX you might have to create an application bundle from your .jar. This process is documented pretty well at the Apple Developer Center (scroll down to "Mac OS X Application Bundles"). You can build a Java app which blends really well into the OSX desktop if you follow what they present there, and it's pretty easy.

Related

JAR File is not Executing

I am Cleaning and Building a project, which is creating its .jar file in its "dist" folder. However the issue is that, I'm not able to run it. I double click on it and nothing happens.
I have set up "bin" folder of JDK in "Path" option in environment variables.
Is there is anything that I am missing? I am new to all this and help is really appreciated.
Suggested
Learn how to make executable file yourself , rather than depending on auto generated jar file
Here is a tutorial.
In case of default jar file ,
most likely you have to execute
java -jar MY_AWESOME_JAR_FILE_NAME.jar
There are 3 common reasons for this:
On windows, and using sysin/sysout
Windows, because apparently microsoft is not capable of fixing ancient silliness, forces upon apps that they either have a terminal in which case an ugly black box always pops up, or they don't, and can't later make one. That means that on windows only, there are 2 java executables: java.exe and javaw.exe, with as only difference that javaw doesn't get a box. But, it doesn't get a box - sysin and sysout do pretty much completely nothing.
By default, double clicking a jar starts it with javaw, which means if your app's only interaction is reading from System.in and writing to System.out or System.err, you won't see anything.
There is no fix to this, other than to make a GUI app, or make a batch file (a windows only concept) that explicitly runs java.exe.
This doesn't apply to linux or macs.
jar file broken
A runnable jar file is one that has three properties:
There is a class inside the jar that has a public static void main(String[] args) method inside.
That class is named (fully qualified) in the manifest of the jar, under key Main-Class
Any deps needed are either baked into the jar, or in another jar, and those other jars are named, space-separated and relative to the dir that the jar you're double clicking lives in, in the manifest, under key Class-Path. Note that the global environment variable CLASSPATH does nothing when double clicking jars.
You can check all this; jars are just zip files. Also, the jar tool in your JDK can unpack them, and the manifest is just a file named META-INF/MANIFEST.MF inside. You can open it up, have a look if it's properly configured.
java install broken
Check any random runnable jar first. Maybe you installed a headless java. Note that these days (since JDK9 and up), an 'end user' wouldn't even install a java and the notion of double clicking a jar to run it is basically obsolete. To make 'desktop' java applications, you'd ship an entire JRE (treeshaken if you want) and you're responsible for an installer. There are some limited tools in the JDK (since 9) that help you out (such as jlink).
Use the CLI to see whether your Java is set up properly, the jar is corrupted, or the jar is incompatible with installed Java version.
Ensure the jar file is properly associated with Java binary. The instruction can be found here
Just edit manifest file adding main method in there. If you have any additional reference libraries simply put them into class path there.
Manifest specificatin is located here:
https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

How to make a .plist for a mac applications

I'm trying to turn a java program into a mac .app file, or more accurately, a native mac application. This requires a .plist file (see here) but I can't seem to find anywhere that works in telling me how to make one. I set up the directories as they specified, and my application debugs properly. Its just every time I try to open the application, it immediately shuts down. Does anyone know how to make a (minimalist even, for now) working plist file?
Right click on your project and select Export. Assuming you on using a Mac, there will be an option to output an application bundle.
Once you have created the app bundle (which itself is just a directory), go here to find the Info.plist file:
MyApp.app/Contents/Info.plist
From there you can edit the plist, if you need to.

How do I create a Windows-like clickable shortcut for Mac OSX?

I am helping a colleague set up some GUI programming tools I have written in Java.
I need to create a clickable icon to run the application which sets the current directory and then invokes the JVM, passing parameters to the program. Also nice would be to be able to use the PNG icon image I created for the program.
I suspect I need to create a script and somehow add that to the launcher strip that's across the bottom of the screen. Once off manual creation is all I need, not programmatic.
What's the right way to do this?
Use the Jar Bundler! With the Jar Bunder you can specify the Main class, any arguments, additional files and resources to the classpath, VM options and many more.
Have a look at this tutorial. There is also an Ant task available (not tried myself yet). If you have installed Xcode, it is located under /Developer/Applications/Utilities/Jar Bundler.app. The Jar Bundler itself is not public downloadable (except the ant task). I'm not sure, if the Jar Bundler is part of the standard Mac OS distribution. Start the terminal and have a look at /usr/share/java/Tools/Jar Bundler.app. You need an apple developer account to get Xcode if it is not present.
if you can do jnlp it will work in both mac and pc world
In OS X the closest thing to a shortcut that I am aware of is an alias. I'm not aware of any Java APIs for creating an alias directly, however you might consider invoking the ln -s command which creates a symbolic link, which is equivilant to an alias.
I can't confirm it works caus I'm on a PC but try
ln -s "/path to/your/app -arg1 -arg2" "alias name"
The OS X documentatiopn for the command is here

How to change default jar icon

First I have read all questions about this subject, so this is not a repeated question.
What I want to ask about, how an application written in Java (such as eclipse) uses an icon instead of the default jar icons.
I don't care about compatibility, I always can make 2 different versions of my program to Windows and Linux.
How to make it in Linux and Windows.
Thanks very much.
In the case of programs like eclipse, which are written in java and have a specific icon in Windows (or Linux or Mac for that matter), they actually have an OS specific executable file, e.g. eclipse.exe for windows, that initialises the java program.
It is this executable that contains the application icon, not the Java program. If you want to do the same, you'll need to create the executable 'front door', as it were, and give it an icon.
If it's just for your own personal aesthetics, just create a short cut to the jar file and change the icon of the shortcut.
To set an image for MAC, you can use com.apple.eawt.Application. try this:
Image img = new ImageIcon("abc.png").getImage(); // your desired image
Application app = Application.getApplication();
app.setDockIconImage(img);
However, this will not build on Windows or Linux by default. If you'd like it to, (Maven) add the following dependency to your POM:
<dependency>
<groupId>com.yuvimasory</groupId>
<artifactId>orange-extensions</artifactId>
<version>1.3.0</version>
</dependency>
Or, if you don't use Maven, you can download the JAR from http://central.maven.org/maven2/com/yuvimasory/orange-extensions/1.3.0/orange-extensions-1.3.0.jar
If the application is launched using Java Web Start, an application icon can be specified for use in desktop shortcuts and menu items.
Note that this icon is not attached directly to the Jar, but since the end user never sees or has to deal with a Jar, that should not be a problem.
I use NetBeans, so if you aren't using that, I don't quite know. But, when you select the window, there is an option in the properties window for an icon file. When you build the .jar, that icon should be built in with the program.
If you're talking about the actual application icon in, say, Windows, you can't change it (programmatically). That's determined by the OS. Although you can change it in your OS in Folder Options.
You can, however, set the window (read: JFrame) icon image using setIconImage().

java:I am trying to create Shotcut of any abc.exe through java program

I am making an installer in java swing it almost completed only one thing is left to do
that is to create desktop shortcut of our software.I do not want to copy software on desktop but I want to create instance of that software like other MS software. How it can be done
please help me.
I am already copied my software in c:/Program files by using copy directory and I want to create shortcut on desktop .
A Windows shortcut is just a .LNK file in the desktop directory. You need to find out the .lnk structure and then you can build such a file from (pure) Java.
Alternative: Use JNI to call the appropriate Windows API.
EDIT: If you want to be lazy, you can create one "template" .lnk file by hand and store it as a resource in your program's Jar; then, for a different abc.exe program name, just copy that file to the desktop directory with the desired link file name, and change the name of the executable inside the file. Hopefully the .exe file name will be at a fixed offset, and hopefully you can control the length of that name to be constant. Then it should be easy.
Shortcut to Create shortcuts ;-)
Here is the Windows file-format for shortcuts (.lnk files).
Simply create a file with the appropriate fields and save it to disk as <ShortcutName>.lnk
Detailed references :
1. [MS-SHLLINK] Official Microsoft Documentation on shortcuts.
2. [reverse-engineered] Windows shortcut file-format.

Categories