- How to run a JAR file on Windows 10 [QUICK GUIDE]
- How can I run a JAR file on Windows 10?
- 1. Add Java to Windows
- 2. Download a JAR file opener
- File Viewer Plus
- 3. Use Java(TM) Platform SE binary
- 4. Run a JAR file from the Windows Command Prompt
- 5. Add a third-party JAR executor to Windows
- Execute jar file as Administrator in Windows
- Executing .exe file from java
- 2 Answers 2
- Not the answer you’re looking for? Browse other questions tagged java windows cmd or ask your own question.
- Related
- Hot Network Questions
- Subscribe to RSS
- How do I run a Java program from the command line on Windows?
- 12 Answers 12
How to run a JAR file on Windows 10 [QUICK GUIDE]
- To run a JAR file on your Windows 10 device you certainly need to have Jave installed.
- You’ll find below a path to the latest Java version in case you need it.
- Then, you can find JAR file openers or executors, and we recommend some.
- Using Command Prompt is also a handy solution if you don’t want to install third-party tools.
A JAR file is a Java archive package format that might include a Java program within it. While you can extract JARs with archive software such as 7zip, you can’t run a pure Java JAR app as you would with other programs in Windows.
Since that’s the case, we’ll cover how you can run a pure Java app JAR file in Windows.
How can I run a JAR file on Windows 10?
1. Add Java to Windows
The crucial thing you need upfront is to have Java installed. Without it, Java apps aren’t going to run in Windows. This is how you can install or update Java in Windows 10:
- First, open Command Prompt(Admin) by pressing the Win key + R keyboard combination and entering cmd in the box.
- Next, input java -version in the Command Prompt and press Enter. It will then provide further details on your Java version as below.
- The current version of Java is Version 8 Update 131. If you don’t have Java or the most up-to-date version, you can add it for free.
- Run through the Java setup wizard. Then, open your browser to verify that the program is working.
2. Download a JAR file opener
You can open JAR files and hundreds of different formats with a single application. File Viewer Plus is a universal file viewer for Windows that can open and display over 300 different file types.
The new updated version has a plethora of features that will save you big time on opening, viewing, or editing files.
File Viewer Plus supports the most popular text, audio or video file type, as well as archive, source code or web files.
File Viewer Plus
3. Use Java(TM) Platform SE binary
- Open the File Explorer and the folder that includes the file you need to run.
- You should right-click the JAR file and select Open with from the context menu.
- Click Choose another app and then select to open it with Java(TM) Platform SE binary.
- Click the Always use this app to open JAR files check box.
- Press the OK button to close the menu.
- Now you can run the selected JAR, and all other JARfiles will also be associated with Java.
4. Run a JAR file from the Windows Command Prompt
- Press the Win key + X hotkey and select Command Prompt (Admin) to open it as administrator.
- Then input the following command (replacing the example with the actual path of the file you need to run) and press Enter: java -jar c:pathtojarfile.jar
- Alternatively, you can open the folder that includes the JAR in the Command Prompt first by entering cd / followed by the directory. Then, you can input java -jar file.jar without the folder path.
That will open the executable JAR file so long as it contains a manifest file to specify the application’s entry point. (The developers should have already included that in the archive.)
If you develop your own Java programs, you’ll need to convert the JAR to an executable format.
5. Add a third-party JAR executor to Windows
There are also executor programs for Windows that enable you to run JAR files the same as any other program . Jarx is one of them and you can add it to Windows by clicking Jarx-1.2-installer.exe on this page.
Click the jarx .exe to run the software, which doesn’t really have a GUI as such (except an About jarx window). Then you can double-click the JAR files to run them in Windows.
If you’re having trouble accessing the JAR file or you get some kind of an error, take a look at this nifty guide to finding some great fixes.
With all this, you can now run JAR files in Windows. If you need to fix a JAR file that isn’t opening, check out this Windows Report guide.
If you have any other suggestions or questions, leave them in the comments section below and we’ll be sure to take a look.
Execute jar file as Administrator in Windows
I know there are tons of questions here asking the same, but in any of those ain’t found the answer that I want, that is a specific way for doing this.
I know that (correct me if I’m wrong):
- For doing this, i’ll need to interact with Windows UAC mechanism.
- This can’t be done with pure Java code, it needs the help of a Batch file, or VBS script.
- A Java running process can’t be elevated to get admin privilegs without needed restart the application.
- Elevate a .jar file will elevate also the JVM and all other process that depends of JVM, with the security concern this imply.
I don’t want to restart any application, my goal is that the Java application ( .jar file) starts from the begining with admin privilegs. If for get that, the users will have to click in some UAC windows, ok, don’t care.
So, my question (that I can’t get and specific answer reading other post asking almost the same thing). How can I do that?, What files I need to create, and with what content? ( .bat or .vbs ), Can I put those files inside my .jar file?, What Java code I’ll need to implement in my app?
Please, be the most specific as possible with the solution. Answers in other post are, or too vague (talk about «possible» solutions, but don’t mention a specific and complete one), or explain too much and don’t give a specific way or code.
Executing .exe file from java
I want to run a .exe file from my java code,along with passing few arguments/options to the .exe file.
So basically, I did following:
NOTE: Here A-B is name of the directory in which xyz.exe is located.
But when the variable out is printed, it actually shows that it has nothing.
So instead of above code I modified it to the following:
Now here when the variable oute is printed, it shows the message,
‘A-B’ is not recognized as an internal or external command, operable program or batch file.
So my question is that why A-B is not being treated as a directory inside which the actual .exe file resides.
Please resolve the error if any one knows about this problem.
2 Answers 2
You should use a full path to your target xyz.exe — when you execute cmd.exe like that, it’s not relative to the folder of your Java program, it’s relative to C:\Windows\System32 therefore it can’t see your A-B folder.
So, for example: cmd.exe /C C:/A-B/xyz.exe health -U admin -P admin
And as @CSK correctly noticed, you can execute your .exe directly, without cmd.exe. For examle:
I know that, for example, Java on Raspbian can’t access directories with a space (for example /New Folder). I guess its some problem about how Raspbian considers the ‘ ‘ char when creating directory. Possibly, Windows might have the same problem with the ‘-‘ char. Can you rename the directory without any such chars and try again?
(I use a Mac so I’m unable to recreate the problem. Another option can be, if this is in fact not a problem to do with chars, to create a shell script from which the exe is executed and instead run this script via Java. I am only advising this because I used this method before without any problems.)
Not the answer you’re looking for? Browse other questions tagged java windows cmd or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How do I run a Java program from the command line on Windows?
I’m trying to execute a Java program from the command line in Windows. Here is my code:
I’m not sure how to execute the program — any help? Is this possible on Windows? Why is it different than another environment (I thought JVM was write once, run anywhere)?
12 Answers 12
Let’s say your file is in C:\mywork\
Run Command Prompt
This makes C:\mywork the current directory.
This displays the directory contents. You should see filenamehere.java among the files.
This tells the system where to find JDK programs.
This runs javac.exe, the compiler. You should see nothing but the next system prompt.
javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.
This runs the Java interpreter. You should then see your program output.
If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!
To complete the answer :
Compile the Java File to a *.class file
- This will create a TheJavaFile.class file
Execution of the Java File
Creation of an executable *.jar file
You’ve got two options here —
With an external manifest file :
Create the manifest file say — MANIFEST.mf
The MANIFEST file is nothing but an explicit entry of the Main Class
jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class
Executable by Entry Point:
To run the Jar File
In case your Java class is in some package. Suppose your Java class named ABC.java is present in com.hello.programs , then you need to run it with the package name.
Compile it in the usual way:
But to run it, you need to give the package name and then your java class name:
Complile a Java file to generate a class:
Execute the generated class:
Assuming the file is called «CopyFile.java», do the following:
The first line compiles the source code into executable byte code. The second line executes it, first adding the current directory to the class path (just in case).
Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.
This was an enhancement with JEP 330: https://openjdk.java.net/jeps/330
For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: https://docs.oracle.com/en/java/javase/11/tools/java.html
It is easy. If you have saved your file as A.text first thing you should do is save it as A.java. Now it is a Java file.
Now you need to open cmd and set path to you A.java file before compile it. you can refer this for that.
Then you can compile your file using command
Then run it using
So that is how you compile and run a java program in cmd. You can also go through these material that is Java in depth lessons. Lot of things you need to understand in Java is covered there for beginners.
You can compile any java source using javac in command line ; eg, javac CopyFile.java. To run : java CopyFile. You can also compile all java files using javac *.java as long as they’re in the same directory
If you’re having an issue resulting with «could not find or load main class» you may not have jre in your path. Have a look at this question: Could not find or load main class
On Windows 7 I had to do the following:
quick way
- Install JDK http://www.oracle.com/technetwork/java/javase/downloads
- in windows, browse into «C:\Program Files\Java\jdk1.8.0_91\bin» (or wherever the latest version of JDK is installed), hold down shift and right click on a blank area within the window and do «open command window here» and this will give you a command line and access to all the BIN tools. «javac» is not by default in the windows system PATH environment variable.
- Follow comments above about how to compile the file («javac MyFile.java» then «java MyFile») https://stackoverflow.com/a/33149828/194872
long way
- Install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
- After installing, in edits the Windows PATH environment variable and adds the following to the path C:\ProgramData\Oracle\Java\javapath. Within this folder are symbolic links to a handful of java executables but «javac» is NOT one of them so when trying to run «javac» from Windows command line it throws an error.
- I edited the path: Control Panel -> System -> Advanced tab -> «Environment Variables. » button -> scroll down to «Path», highlight and edit -> replaced the «C:\ProgramData\Oracle\Java\javapath» with a direct path to the java BIN folder «C:\Program Files\Java\jdk1.8.0_91\bin».
This likely breaks when you upgrade your JDK installation but you have access to all the command line tools now.