Suppose I made a program to take two input and display the sum.
It runs in netbeans but when I build out the jar file , and
double clicked the jar file , nothing happens.
How can I make these program run?
You must run this from command line, and then execute java -jar {PATH_TO_JAR}
You can use the command-line :
java -jar <yourjar> <yourMainClass>
You can also add the main class in a manifest file into the jar. Doing so, you don't need to specify anymore on command line
Jar files needs to run in JVM - Java Virtual machine, to run your file use:
"java -jar" and your jar.
By doing so, you tell Java to run your jar in jvm which means it now can run.
Related
There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.
I was able to successfully create a .exe file from an executable jar. From Launch4j I can test the wrapper, and the output on the log is what I expect. However if I try to run the exe from the command line or from Windows Explorer, nothing happens. No error, no output to the console as expected. The program is also supposed to edit a text file which does happen when I run the jar using a batch file, but not when I run the exe. This is all on the same computer so I doubt it is a problem with the JRE. I have searched StackOverflow extensively but found nothing that helps with this situation. I did find this post with a similar problem: Launch4J executable not executing as expected but nobody actually answered the question. Thank you in advance for helping
I found the issue. Under Header, I had to switch the Header type from GUI to Console. After that I was able to run the exe.
You may be accepting something as a command line argument which may be throwing an error like in my case .
For my case, it was the tick on "Signle Instance" tab: Allow only a single instance of the application.
Although I had killed the process, for some unknown reason the exe had been recognized as alive. Thus, a reboot of the PC is recommended.
These are the things I had to do for it to work on Windows 10:
First, make sure the executable JAR you created actually executes. I could never got the JAR to execute by double-clicking. Instead, I created a .bat file where I added the java -jar command-line instruction to execute the jar, including the VM arguments. For example, to execute foo.jar, the .bat file should contain java -jar --module-path %FX_HOME% --add-modules javafx.controls,javafx.fxml foo.jar. Once you get the JAR to execute without errors (pay attention to the command prompt window), then your JAR is ready. Then, do the following from the Launch4J app:
Create FX_HOME environment variable. Make sure it points to the lib folder of the downloaded Java FX distribution is located.*
Set JAVA_HOME environment variable.*
Make sure JAVA_HOME is the first entry on the Path variable. Verify by opening a command prompt and typing where java and hitting the ENTER key.
Under Header, make sure "GUI" is selected.
Under JRE, set Bundled JRE paths to %JAVA_HOME%.
Under VM Option, enter --module-path %FX_HOME% --add-modules javafx.controls,javafx.fxml
*If any of the paths contain spaces, make sure they are in quotes when you create the environment variable. For example: C:\Program Files\Java 17\bin, should be in quotes. This is because the command-line parameters are space-delimited and "Program Files" contains a space, fooling the command-line interpreter to think Program is the end of one parameter and Files is the beginning of another. By putting in quotes, the interpreter now knows the space is part of that single parameter that represents the path.
I am scheduling a task (with windows task scheduler) which simply run a batch file.
this batch is running a jar file (Java program) with a simple "Java -jar test.jar".
When i run the script from the command line manually, the java program runs well and no exception is shown.
but when the task schedular does the same (calling the command), the java program ends with an exception: "ClassNotFoundException" for one of the classes which is in my jar.
What way be the cause of this? what is the diffrence when calling the jar from the command line and from the task scheduler?
Thanks.
I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. In the first line of your .bat, can you do a cd \path\that\you\expect before you execute java?
Does your jar have any dependencies? Also, it'd be helpful to know what's your folder structure, and how exactly do you run it in the command line.
Anyways, depending on your case, you can do something along these lines:
cd /path/to/exec/folder // set current directory
java -cp /all-classpath-jars/and-or-bin-folders/ test.jar your.package.MainClass [args...]
This has to work, if you specify everything you need correctly.
I am trying to write Java console application, the code is quite simple:
public class ConsoleTest {
public static void main(String[] args) {
System.out.println("test");
}
}
If I run this application from Eclipse, then i see "test" in Eclipse's "Console", but if I export my app as a "Runnable JAR file" and run it from Windows XP cmd.exe, then nothing is echoed to the console.
On the safe side, i tried to check System.console(), it returns null.
What can be wrong?
How are you running your program outside eclipse?
You should use command line like
java -jar yourjar.jar
or
java -cp yourjar.jar ConsoleTest
If you are occasionally using javaw instead no console output will be produced. The STDOUT of javaw is null. Probably this is what happens when you are clicking on your jar file.
You need to run your jar file from the command line.
If you double click on it, you wont be able to see the command line operations being run in the background. Jar files are usually run by double clicking only when they involve GUI.
To run a jar file from a command prompt, just do this:
java -jar ConsoleTest.jar
Assuming you've set environment variables for java.exe and the current directory has the jar file.
If this doesn't work, then it is likely not your code's fault.
There is also the chance that the manifest file pointing to the Main class was set up incorrectly.
Try compiling it into a class from the command line and then running it. Does that work?
javac ConsoleTest.java
java ConsoleTest
Chances are your runnable jar file isn't working as you expect and isn't actually running your ConsoleTest class.
Like what Hovercraft Full Of Eels wrote, compile the .java file using cmd.exe with the following command:
javac ConsoleTest.java
Then this will create a .class file, and we want to compile it with this command:
java ConsoleTest
It should then display the "test" output.
you have to install the java jdk and add the root of the directory containing the file java.exe to your system's directories, you will find further informations in the jdk's installation guide.
and then run the file in the console with the command
> java file.java
I have an executable jar that I know is executing, because I put two distinct beeps at the beginning of the code that I hear whenever I double click on it or when I run it through the cli. Even when I run it through the command line however, it does not display output nor prompt for input when I use System.out/System.in respectively. Everything is functional when I run it through eclipse.
How do I get the .jar to output/input to the same command line I executed it in?
Assuming java is in your PATH, you should lauch your program as follows :
java -jar myProgram.jar
If you want to run it without opening the command line, you could create a .cmd file in the same directory with this content :
#echo off
java -jar myProgram.jar
pause
then, double-click on the .cmd file.