6

The Problem is as follows: I have a Java Application packed in a ".jar"-File.

I can start the App (Swing Application) by either

  • doubleclick on the jarfile
  • commandline java -jar MyApp.jar

However when I put the commandline command into a batchfile, it will tell me

Unable to access jarfile MyApp.jar

The batch file is in the same directory as the jar. Just where I also started from cmd-line. I tried to call the bat with Administrator-rights and from an admin-shell - no effect.

I'm working on a Windows 7 (64bit) machine. Any hints will be appreciated.

EDIT

You asked for it :) Here is the batch file:

@ECHO OFF
java -jar MyApp.jar
@PAUSE

EDIT 2

Invoking the batch by double-click and from cmd has the same effect. Changing the command to

java -jar "C:\Full\Path\MyApp.jar"

has no effect.

EDIT 3

I added SET Path=%Path%;C:\Path\To\Jar to the batchfile. No effect. I also tried using double backslashes as suggested by Chris' link and supported by Viruzzo. Also no effect.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 2
    can you post the batch file ? – Sibster Apr 17 '12 at 14:44
  • How are you invoking the batch file? – vaughandroid Apr 17 '12 at 14:46
  • This may be an ignorant suggestion but did you chmod ### the relevant file(s)? Edit: "chmod-equivalent command", I'm still processing my first cup of coffee today, thanks @Viruzzo – Chris Apr 17 '12 at 14:49
  • @Chris chmod on Windows? – Viruzzo Apr 17 '12 at 14:50
  • @Chris: As I wrote, trying around with rights, did not succeed. I guess it must be something about paths. I just cannot think of more than classpath, path, working dir ... – Fildor Apr 17 '12 at 14:59
  • [Have you tried something like this (check comments)?](http://stackoverflow.com/questions/2622062/batch-file-command-to-run-jar-file) – Chris Apr 17 '12 at 15:06
  • what happen if you set XP compatibility mode on that batch file and execute it on Explorer? – kingori Apr 17 '12 at 15:21
  • I did not expect too much, but for the fun of it I tried XP-Compat-Mode. It absolutely had no effect. – Fildor Apr 18 '12 at 07:23

5 Answers5

3

When you double click on .bat file, the working directory is not set to where the .bat file is, but is usually something like %AppData%. To make it work you need to put the full path to the file inside the bat (i.e. java -jar C:\path\to\jar\MyApp.jar).

Viruzzo
  • 3,025
  • 13
  • 13
2

I just had to put my path in quotes, even though it had no spaces or odd characters.

java -jar C:\Users\Jacob\executables\some-file.jar failed, but

java -jar "C:\Users\JStamm\executables\some-file.jar" worked.

Jacob Stamm
  • 1,660
  • 1
  • 29
  • 53
  • Strangely it does not work with single quotes. With double quotes everything runs perfectly! – phylib Dec 15 '22 at 13:36
1

This isn't the answer to your specific problem, but this will allow you to troubleshoot the problem so you will find an answer. Adding a sleep command will let the command prompt sit, so it doesn't close right away.

ping 1.1.1.1 -n 1 -w 10000

(sleep for 10 seconds)

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
0

Make sure you don't have the hide extension option enabled in windows. Maybe the name of your jar ends with .jar.jar? Are you sure the path to the jar is correct?

ppalasek
  • 372
  • 2
  • 12
0

I havent't yet figured out why, but now it works:

The first batchfile was a copy of another and I just changed the contents to fit my needs.

NOW I renamed it to MyApp.bak and wrote a new MyApp.bat from scratch.

As soon as I find out, what the difference was, I'll update this answer.

UPDATE

It happened again with another batch. This time, we got the reason: For some reason, the batch file did not inherit rights when copied. Creating a new one in the folder had the correct rights. So, in the end it was indeed a rights-issue.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • @Pinhassi Unfortunately not. I guess I won't have the time to further investigate, neither. – Fildor Dec 13 '12 at 13:50