File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes launching jar from script.bat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "launching jar from script.bat" Watch "launching jar from script.bat" New topic

launching jar from script.bat

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi Guys,

Perhaps my computer is cursed with some anomally, but for the life of me I can't figure out this strange phenomenon. Perhaps one of you have come across this?

I've completed my project and have placed it in a jar. I can open a command window and launch the app using:

javaw -jar pns_1.0.jar

It launches, no problem. This app immediately creates a file called profiles.dat. And I am able to bring up a JFileChooser to save a file. All this when my approach to launching the app begins with the use of the command prompt window.

Now the interesting stuff begins... I created a script.bat file that does the launching for me. A Shortcut on the desktop is linked to the script.bat file so that I can launch the app from there. The script looks like so:

@echo off
if "%JAVA_HOME%"=="" call:FIND_JAVA_HOME
rem echo "Java home: %JAVA_HOME%"
rem Start your java application or perform other actions here
"%JAVA_HOME%"\bin\javaw -jar pns_1.0.jar "com.personalnetsearch.parser.MainGUI"

FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion') DO set CurVer=%%B

FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment\%CurVer%" /v JavaHome') DO set JAVA_HOME=%%B



The script launches the app but the app doesn't produce the profiles.dat file as it should and I'm not able to save a file with JFileChooser. JFileChooser simply doesn't open when I select the File/Save As menu. No exceptions are generated. I've gone to the root directory of the application and made sure that all security permissions were set to allow all users read, write and execute privilages. And did the same on the script file itself. All privilages are set as they should be. It's strange that when I launch the app using the command window everything works as expected, but when I use the script.bat file, it doesn't want to play nice :-(

I even thought of swaping out the code in the script file to something very basic and direct like:

"C:\Program Files\Java\jre1.6.0_03"\bin\javaw -jar pns_1.0.jar

Again, this launches the app, but profiles.dat is not created and the JFileChooser refuses to open.

If anyone has any suggestions I can look into to rectify this issue, I'd appreciate it.

Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

Hi Alan Shiers,

I'm not an expert in batch scripting, and I'm also reading about such issue for the first time.

Anyways, you can do below things (its not a solution, but will help to approach the issue):

1) Try by using 'start' command in script (I assume you are working on Windows flavour and .bat file)
2) Try including javaw in your PATH variable.
3) Try enabling debugging log (or at least put the logs at problematic locations like listeners, file choosers etc.)

Let's see what happens.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

I had some time to play some more with this issue. I believe the whole thing has a lot to do with the fact that recently I upgraded my OS from Windows Vista to Windows 7. I didn't just wipe the drive and start fresh, which is what I should have done in retrospect. I actually just had the install disc upgrade over top of the existing OS. End result: it created an issue with security permissions. My problem became two fold. I rectified both issues by:

1. Reinstalling my software to a different location on the hard drive.
2. Enabled debug error messages on the software so that I could see what exceptions were occurring.

I had to play with the OS security permissions where the application resides, and made small modification in my code after viewing stacktrace. It's all working fine now. Thanks for your guidance. This is an awesome forum. I get a lot of issues rectified through here.

Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

You are welcome.

I'm glad that your problem is resolved.
I agree. Here's the link:
subject: launching jar from script.bat
It's not a secret anymore!