Unable to run jar file from command line - java

I have an excel spreadsheet which launches several jar files from the command line. For some reason one of my jars has failed to run from the command line, yet when I double click on the executable, it runs fine.
I need the sheet to be able to run it from the command line. What could the issue be?

There are certain steps you need to perform to run jar through command line without specifying main class.
Add following line to your manifest file , while creating jar.
Main-Class : mypackage.myclass.yourclassnamehere
Here is the link [ http://www.mkyong.com/java/how-to-make-an-executable-jar-file/ ] for sample tutorial.
If you want to run from excel file, add java.exe as default program for jar extension executable. (In windows you can do it from control panel or right click on executable and select java.exe from other programs and ticking option as default program for .jar extension)

Related

How to call a main class in a jar package from eclipse?

Currently, there is bat file that calls the main class in a jar file. Now I want to run it using eclipse. How do I configure the eclipse to run it?
I have tried the Run > External tools > External tools configuration. But I don't know what to type in...
#echo off
set MODULE2_HOME=%~dp0..
set JAVA_HOME=C:/Program Files/Java/jdk1.6.0_71
set CLASSPATH="%MODULE2_HOME%/classes;%MODULE2_HOME%/lib/*;%MODULE2_HOME%/lib/oracle/*;%MODULE2_HOME%/lib/aspose/*;%MODULE_HOME%/aspose/*"
set SETUP_PROPERTIES="%MODULE2_HOME%\conf\setup.properties"
set PATH=%JAVA_HOME%\bin;%PATH%
java -cp %CLASSPATH% -Dsetup.properties=%SETUP_PROPERTIES% com.module.fast.main.Module2Main %*
How do I configure eclipse to run exactly like this command?
Since it is a batch file which will run in Windows. You can go to the command prompt and run the batch file using .bat. You do not need Eclipse. You can run the java class in eclipse. Still if you want to run, you can check the following screenshot.
While configuring, make sure to give the complete path of batch file. If the bat file needs extra commandline params, you can give the batch file location with space and the command line parameters separated with spaces.
It does not work to run the jar file which contains main class, you have to give the working directory location.

Creation of a .bat file of a java application

I am a bit confused about the process to create a .bat file of a java application. I have exported the executable jar using IDE say Application.jar in C: directory. Then I have written two lines in a .txt file as stated below and saved it as .bat file in the same directory where i have my application.jar. But on double click of the .bat file, the application is not getting executed.
.BAT file code
javac Application.java
java -cp . Application
Note: I have also set the JRE and JDK path in my environment variables till bin path in My Computer properties. But it is not working. Can someone suggest me how can I fix this, because I want to execute my code by doubleclickng on a .bat file. It will be nice if someone can provide me every step I need to follow to accomplish this as I havent ever done this before.
Thanks ,
The first line in your batch file is attempting to compile your program !?
The second line is attempting to run the Application.class file.
What you want if you have produced an executable jar file is:
java -jar Application.jar
But you don't really need the batch file at all. If you double click on the jar file and it runs your program then you can just create a shortcut to it.
Your .bat is just fine. When you double click it might be executing and then closes. This is because your program might not have any UI and it isnt waiting for any input. To verify this take a command prompt and then execute your bat file via that.
In other case I assume that you have a java class called Application and you need to run this via a batch file. In that case if the class have a main method then you just need one line in .bat file
java -cp <the path to class file> Application
So you might be using a javac just to take advantage of class path as current directory. So when you say
javac Application.java
java -cp . Application
It compiles the class to current folder and set that as class path and then execute. This is absolutely file as long as the Application.java doesnt have any third party dependency. But in this case again you need not set -cp to . (current directory will be taken as classpath automatically unless otherwise specificed). So below will also work fine.
javac Application.java
java Application
I support Jurgen reply. If you have an executable jar file and a jre in path then double clicking it will run the application. The META-INF folder inside the jar will have a MANIFEST.MF file which uses a property called Main-Class: to specify the main executing class. And on double clicking this class gets executed. However its only useful if you have a UI. Else it'll also have no effect.
In all these context the Application.jar you mentioned is irrelevant. If that is a third party jar that you need to run the you should include that in -cp argument.

Jar file won't run via command prompt or double-click

I recently finished a project which works as it is supposed to in my Eclipse IDE as both multiple files and as a single file.
Eclipse exports the jar file and only makes noise about the warnings.
When I go to run the jar file with a double-click, the cursor seems to flash to a hourglass for less than a second and then nothing. When I try to run the jar file from the command line with java -jar myJarFile.jar the command prompt window seems to wait a second and then brings the file path line and cursor with no errors and no other messages.
I have double checked both my Path variable and that I have the latest version of Java installed.
Any suggestions?
Check this thread: Failing to execute the jar file using java -jar command
Basically, what you need to do is when you run the jar, you need to specify the class to be executed in the command line, like so:
java -cp test.jar com.app.ClassName
Did you just make a jar file, or a runnable jar file? If you only did the former then it will not execute on a double click. So it's Right Click the project in Eclipse -> Export -> Runnable JAR File -> Choose the appropriate Launch Configuration -> Choose where you want it to go.
Confirm that your application code is in the jar file:
jar tf myJarFile.jar
Confirm that your application's main class is listed in the Main-Class attribute in the manifest, and that any other jar files needed by your application are listed in the Class-Path attribute:
jar xf myJarFile.jar META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF
Try running with verbose logging of classes being loaded; this should show if some necessary class can't be found:
java -verbose:class -jar myJarFile.jar

Launch Eclipse Program Exported From File

I already read many questions it the same subject, but none solved my problem..
I know that I can easly launch my app using this command on console java -jar myappname.jar
But what I want is to click on my .jar file exported by eclipse and it launches console with my app inside, do u understand?
I done the export using this configs :
File>Export>Jar File
Selected all the classes of my project
Selected "Export generated class files and resources"
Selected "Export java source files and resources"
Selected "Compress the contents of the Jar File"
Pressed Next
Selected "Export class files with compile errors"
Selected "Export class files with compile warnings"
Pressed Next
Selected "Generate the manifest file"
Selected Seal the Jar
And on "Select the class of the Application entry point:"
I choose my class where is the void main method .
the jar appears on my desktop, but then, when I double click it doesnt launch the console. why??
Thanks in advance!!
Launching a jar by double-clicking the file (or shortcut) will not display a terminal. One workaround is to change the default execute action in your operating system for .jar files to open a terminal and execute the command from within the terminal. A script like the following might do the trick (using Bash):
#!/bin/sh
/usr/bin/gnome-terminal -x java -jar $*
sleep 3
Then right-click on the jar file and choose the script as the default program to run for that file type.
Disclaimer: the above script actually fails for me. It works fine if the command being run in the terminal is "top", so it looks like you may need to tweak this a bit.
It depends on your OS.
If you use Windows, you can create .bat or .exe files. You can find 'how-to-create' tutorials on the internet.
If you use Unix based OS, you can just set the jar to be the executable.
A third party OS probaly has it's own way to set to executable.
This is the only way I am aware of.

JVM Launcher cannot find main class

I am using NetBeans to build my executable JAR and haven't messed around with any Ant or project settings (using defaults).
When I go to Run >> Clean and Build Project, I see NetBeans building my JAR and putting it into my project's dist/ directory without a hitch.
When I double-click that JAR to run it, I get the following error message:
Could not find the main class: com.me.myorg.MainApp. Program will exit.
If I open up the JAR in WinRAR and extract the META-INF/MANIFEST.MF file, I see the following attribute defined:
Main-Class: com.me.myorg.MainApp
Obviously, com.me.myorg.MainApp is the "head" of my GUI (Swing) app.
Any ideas as to what is going on? How to troubleshoot? Thanks in advance.
Try in command prompt(Windows):
ftype jarfile="C:\PATH_TO\javaw.exe" -jar "%1" %
EDIT
Run the command prompt like this: Go to Start and in the Search box type CMD. Right click the Command Prompt icon and choose Run as Administrator.
http://www.howtogeek.com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-run-box/
Then run the command above.
Is there a new line after the main class declaration?
Main-Class: com.me.myorg.MainApp
# empty line here
The manifest file format requires each line to end with a line separator. If the line with main class is the last line in the file it is ignored.

Categories