Running .jar file - Double click vs. command line execution - java

I have a java desktop application that contains the following code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println("check1");
int intResult = compiler.run(System.in, System.out, foutErrorFile, strvalidatePath);
System.out.println("check2");
When I run the corresponding .jar file of this application by executing "java -jar name.jar", both check1 and check2 gets printed and app works fine.
But when i try to run the jar by double clicking the .jar file, I found that ToolProvider.getSystemJavaCompiler() is returning null. "check2" does not get printed. I dont get proper result from compiler.run().
I did modify the registry entry "\HKEY_CLASSES_ROOT\jarfile\shell\open\command" from
"C:\Program Files\Java\jre1.6.0\bin\javaw.exe" -jar "%1" %* to
"C:\Program Files\Java\jre1.6.0\bin\java.exe" -jar "%1" %*.
This way I'm able to see the console when the app is running.
So why is my program (which runs fine while run using java -jar command) malfunctioning when I run the .jar file by double-clicking?

I got my problem solved. While double-clicking, the command that gets executed is the one specified in registry entry. In my case the registry entry "\HKEY_CLASSES_ROOT\jarfile\shell\open\command" is:
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
This means its uses the javaw.exe app in the JRE directory to execute the program. My JRE directory lacked one .jar file named Tools.jar in its lib folder. This was essential to acquire the compiler during the program execution.
I copied the missing jar file from the JDK directory lib folder to the same in JRE directory. This solved my problem. Thank you to all for helping.

I think the best way is create a .bat file who calls java -jar name.jar

Related

Executable jar not running? Windows 10

I am having various .jar files in my system.
I have the JDK and JRE installed.
Most of jar files run on double clicking, but there are 2 - 3 jar files which do nothing on clicking. Help me.
By the way I am using windows 10 64 bit
Problem:
I could not start JAR files just by clicking on them in Windows 10.
I was messing around with the "control panel" ... forget it.
What I've found out:
Start a command line (cmd) as an Administrator
Check your file type association:
ftype jarfile
assoc .jar
I had to change my java path to a different one
ftype jarfile=C:\myjavapath\javaw.exe -jar "%1" %*
Which basically means, that if someone starts a jar file, the command would be:
C:\myjavapath\javaw.exe -jar "example.jar" parameter1 parameter2
For me, I also had to change my .lnk files to only contain the name of the jar file, not the whole command. Type in the whole path of the jar file in the "target" field in the properties of the link file (.lnk).
You can debug it in the Command Prompt.
Open start, type in CMD.exe, hit enter
Then, type in
java -jar "path\to\file.jar" without the quotes
or
java "path\to\file.jar"
You should be able to see an output log of what is happening that is making the jar file not execute properly
Your Manifest file should contain a line:
Main-Class: classname
See "Setting an Application's Entry Point".

Opening a .jar file

I can't open .jar file on Windows 10 (I have tried everything like reinstalling Java and setting the default app to open it to be javaw.exe) and I don't exactly trust jarunner/jarfixer so I want to make a batch program which would detect whenever a .jar is being opened it would get the path of it and would run java -jar [pathtofile].
You'll need to execute this as administrator:
ASSOC .jar=jarfile
FTYPE jarfile=c:\java\java.exe -jar "%1" %*
setx PATHEXT "%PATHEXT%;.jar"
Just put the correct path to the java.exe . Mind that you'll have to keep all your dependencies in the %classpath% vatiable.
More for ASSOC and FTYPE commands.
I am not sure if this is related, but I used this tool http://launch4j.sourceforge.net/ to make .exe from .jar

Java: Unable to access jarfile when running as an admin

I have a jar file named test.jar, which I am running with a batch script from the same folder.
Here's the batch code:
java -jar test.jar
pause
The jar itself works with no problems, and I can run it just fine.
However, if I try to run the batch file as an administrator (by right clicking it and choosing "Run as Administrator"), I get the following error:
Error: Unable to access jarfile test.jar
I'm using Windows 8.1, but this also happened on a machine running Windows 7.
What can I do to be able to run this as an Administrator?
i had the same problem that you and i solved it by changing
java -jar test.jar
to
java -jar %~dp0test.jar
%~dp0 holds the directory of your bat file (AFAIK) so %~dp0 will give Java the full path to the jar file solving the problem.
You could also ad a temp path to java
path=C:\Program Files\Java\jre1.8.0_40\bin
Java script
path=

Running program as jar file

I have Java GUI application in Java SE version 7 which I run on Windows 7 64 bits. When I try to run it clicking on jar file the pop up Window is saying 'Cannot find the main class or load it:...'. When I run the same jar file via command line in this way: java -jar app.jar' it works fine. If you try to run it in this way: 'java app.jar' it throws the same error. On Eclipse IDE it works fine. The jar file has been created by the Eclipse IDE as Running jar file. I have created one more project with simple GUI. It has the same problems as above example. What might be the problem? My goal is to run the app once you click on the jar file.
Best regards
Sounds like on your system the .jar extension is registered to run with Java6 and your application needs Java7.
On the commandline type the following command to find out how the .jar extension is registered:
assoc .jar
It will show something like this:
.jar=jarfile
(Note: on your system it might be a different type name. Anything after the = is the typename that you need to use)
Now you need to find out which command is associated with the typename jarfile by using:
ftype jarfile
On my system it shows:
jarfile="C:\Programme\Java\jre7\bin\javaw.exe" -jar "%1" %*
If that is not pointing to a Java7 installation you need to change that e.g. through the ftype command or through the Control Panel.
You might also be able to switch the default Java VM by using the "Java" applet in the Control Panel.
You need to make a manifest file, under the Meta-INF folder. If there's already one there, add the line
main-class: [class name].class
And try that (not sure if there's supposed to be a space after the colon)

Java: System.out.println() should write to the console, but it doesn't

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

Categories

Resources