Starting Java app from cmd works, from batch not
Asked Answered
A

5

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.

Affirmative answered 17/4, 2012 at 14:41 Comment(8)
can you post the batch file ?Minesweeper
How are you invoking the batch file?Prasad
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 @ViruzzoProbity
@Probity chmod on Windows?Countryman
@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 ...Affirmative
Have you tried something like this (check comments)?Probity
what happen if you set XP compatibility mode on that batch file and execute it on Explorer?Forefinger
I did not expect too much, but for the fun of it I tried XP-Compat-Mode. It absolutely had no effect.Affirmative
A
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.

Affirmative answered 18/4, 2012 at 7:47 Comment(1)
@Pinhassi Unfortunately not. I guess I won't have the time to further investigate, neither.Affirmative
C
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).

Countryman answered 17/4, 2012 at 14:47 Comment(4)
Try moving the file in the simplest directory possible (i.e. C: or a new directory under that) and try again. There may be problems with the specific path.Countryman
Moved it to C:\test\ and changed the command accordingly. No effect :(Affirmative
One of the comments in the thread @Probity linked points that you may have to use double slashes in the path.Countryman
This is not very portable solution.Rondel
S
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.

Stenson answered 26/6, 2018 at 21:11 Comment(1)
Strangely it does not work with single quotes. With double quotes everything runs perfectly!Metabolic
S
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)

Sungsungari answered 10/5, 2015 at 9:30 Comment(0)
A
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?

Albuminuria answered 17/4, 2012 at 15:2 Comment(2)
That's the first thing I do, when I get my hands on a new Computer :)Affirmative
You can maybe try adding cd C:\path\to\jar before the java -jar MyApp.jar command.Albuminuria
A
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.

Affirmative answered 18/4, 2012 at 7:47 Comment(1)
@Pinhassi Unfortunately not. I guess I won't have the time to further investigate, neither.Affirmative

© 2022 - 2024 — McMap. All rights reserved.