简体   繁体   中英

How do I run a java application like I would a typical program, using a shortcut?

I'm currently using an IDE for writing a java application and testing it. In the IDE, I can run the application and see how it works. However, how would I run the application using a shortcut, or a jar file? For example, in order to run my WAMP server, I run the wamp.exe file in the WAMP directory. So, I'm running a single file which launches the entire program. How do I achieve this sort of thing with a java application? I've heard about using a jar file, but I'm unsure about whether that would be the proper way to do this or not.

It depends on the IDE you are using. With eclipse for example, you open up the file tab, select export, open java in the tree, and select runnable jar file. Then fill the interface out and your good to go.

If you are using Eclipse, then you can export your application as a single jar file and run it directly by double clicking

In the Package Explorer view:

  1. Right click on your project
  2. Go to "Export"
  3. Click on "Export as Runnable Jar"

And you are done

And if you are using Netbeans, then follow these steps:

  1. Right click on your project
  2. Click on "Clean and Build"
  3. Now got the directory where your netbeans projects are created( usually it should be "C:\\Users\\your_user_name\\Documents\\NetBeansProjects"
  4. Open the directory of your project (directory with the name of your project name)
  5. Open "dist" folder and you'll find the jar file of your application/project there.

Java Web Start is the easiest way to add shortcuts for a desktop app.

Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

See also

This answer - the 2 icons on the right "JotPad" & "Star Zoom Animation" are icons installed by JWS.

You can also, just for fun, create a launcher in C++ that launches your executable(the way Limewire did it). A simple console mode C program is given below.

#include <stdlib.h>

int main(int argc, char *argv[]) {
    system("java YourProgramClassFileNameHere");
    return 0;
}

An advanced methodology would require CreateProcess() in win32API or in JNI for your platform.

You can convert it to an executable Jar file(See Abu's answer)

You can also, create a bat file which can run your program: (Windows only)

@echo off
start java YourProgramClassFileNameHere

Or a shell script(BASH)

Also, see here run a executable jar from c++ code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM