Executing a jar via batch

Hi,

I have an application which generates a jar. I need to run this jar via a batch file. AFter tinkering with the classpath for god know how long, I have finally been able to execute the jar as follows:

java com.oberon.runtime.ZipFileRuntime insertDB.jar where "com.oberon.runtime.ZipFileRuntime" is the main class as indicated in the manifest.mf

I placed this line "java com.oberon.runtime.ZipFileRuntime insertDB.jar" in a file called test.bat. But when I db clik this batch file it starts to throw an exception again :
Exception in thread "main" java.lang.NoClassDefFoundError: com/oberon/runtime/ZipFileRuntime

The strange thing is if I go the command prompt and type test.bat, it runs great ! without any exceptions.

Whats going on ??any suggestions ??

thnx
LVL 1
sands76Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>
Can anyone pl explain why the cmd "java com.oberon.runtime.ZipFileRuntime insertDB.jar" works fine when run at the cmd line but fails when executed via a batch file ? (the batch file is located in the same dir as the jar)
>>

There's only one explanation i can think of - the batch file is not being run in the same terminal process as the 'manual' command and is therefore not dealing with same environment variables (such as classpath). Perhaps you're double-clicking on its icon to run it?

Issue the following command in your 'manual' terminal window:

echo %CLASSPATH%

and then make sure the same classpath is set in your batch file:

SET CLASSPATH=xxxxx
0
 
CEHJCommented:
If executable jar

java -jar <jarfile name> <fully-qualified main classname>
0
 
CEHJCommented:
Sorry

Executable jar:

java -jar <jarfile name>

Non-executable:

java -classpath <jarfile name> <fully-qualified main classname>
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
sands76Author Commented:
Nope.. I had already tried the above and it doesnt work.. Keep getting the exception..

The only thing that really working is executing the line java com.oberon.runtime.ZipFileRuntime insertDB.jar at the cmd prompt.. :(



0
 
CEHJCommented:
Sorry - losing concentration ;-)

Simply put the same command you're using successfully into the batch file
0
 
sands76Author Commented:
that was the first thing I tried.. its not working :(

argh. i hate classpaths!
0
 
zeus_akkaliCommented:
Try this -

java -classpath <fully qualified path/jarfile name> <fully-qualified main classname>
Make sure you have the class name come at the end.

-Zeus
0
 
sands76Author Commented:
nope.. keep getting the NoclassDefFoundError exception.

Can anyone pl explain why the cmd "java com.oberon.runtime.ZipFileRuntime insertDB.jar" works fine when run at the cmd line but fails when executed via a batch file ? (the batch file is located in the same dir as the jar)

thnx.
0
 
WebstormCommented:
Hi sands76,

Try:
    java -classpath insertDB.jar com.oberon.runtime.ZipFileRuntime
Or:
    java -cp insertDB.jar com.oberon.runtime.ZipFileRuntime

Also, you can execute the following command:
    jar -tvf insertDB.jar
to check that the file com/oberon/runtime/ZipFileRuntime.class is in the jar file

0
 
objectsCommented:
How are you running your batch, any other command preceding the java call
0
 
sands76Author Commented:
Hi,

My problems are increasing.:(   I booted into my machine today and tried executing the jar file from the cmd prompt as I had done of Friday but its not working..

echo %CLASSPATH% reads like this:

C:\jdk\lib\classes12.zip;C:\tomcat5\common\lib\servlet.jar;c:\jdk\bin;c:\jdk;C:\jd
k\lib\tools.jar;C:\tomcat5\webapps\TMN\WEB-INF\lib\ojdbc14.jar;f:\tmn\junk\

f:\tmn\junk\ is the folder where I have the jar I want to execute. After navigating the this folder I also tried typing

F:\TMN\junk>set classpath=%CLASSPATH%;. &

F:\TMN\junk>set classpath=%CLASSPATH%;insertDB.jar but still keep getting the same exception..

Isnt there any surefire way of dealing with this problem once and for all ????

thnx,

0
 
objectsCommented:
what command did you use
0
 
objectsCommented:
should be:

java <classname>
0
 
CEHJCommented:
>>f:\tmn\junk\ is the folder where I have the jar I want to execute.

Yes, but just having the folder won't do. Jars are not picked up by folder name. You need to put the full path to the jar
0
 
CEHJCommented:
>>Isnt there any surefire way of dealing with this problem once and for all ????

(Yes - making sure you have the right classpath - including full paths including file extensions where jars are concerned)
0
 
objectsCommented:
btw, if you put your jars into your jre(s) ext directories then you don't need to worry about putting them in your classpath.
0
 
objectsCommented:
what exactly is the exception you are now getting?
0
 
sands76Author Commented:
Ok, I did the fwg:

F:\TMN\junk>echo %CLASSPaTH%
C:\jdk\lib\classes12.zip;C:\tomcat5\common\lib\servlet.jar;c:\jdk\bin;c:\jdk;C:\
jdk\lib\tools.jar;C:\tomcat5\webapps\TMN\WEB-INF\lib\ojdbc14.jar;f:\tmn\junk\

F:\TMN\junk>set classpath=%CLASSPATH%;F:\TMN\junk\insertDB.jar

F:\TMN\junk>echo %CLASSPaTH%
C:\jdk\lib\classes12.zip;C:\tomcat5\common\lib\servlet.jar;c:\jdk\bin;c:\jdk;C:\
jdk\lib\tools.jar;C:\tomcat5\webapps\TMN\WEB-INF\lib\ojdbc14.jar;f:\tmn\junk\;F:
\TMN\junk\insertDB.jar

F:\TMN\junk>java com.oberon.runtime.ZipFileRuntime insertDB.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/oberon/runtime/ZipFileRuntime

What more can I do ? tried using the cmd -jar etc.. but nothing seems to work.. The fact is when I could make it work on Friday, it was by repeating what I have done above several time and it suddenly began to work..

thnx again,
0
 
CEHJCommented:
>>com.oberon.runtime.ZipFileRuntime

What jar is that class in?

Can you prove it by

jar -tvf someJar.jar

?
0
 
objectsCommented:
try:

java com.oberon.runtime.ZipFileRuntime
0
 
sands76Author Commented:
@CEHJ - yes, jar -tvf list the classes I need in insertDB.jar

and @objects - just java com.oberon.runtime.ZipFileRuntime gives an exception.

Ok, I recompiled a new jar  and placed it in a different directory and when running it got a new exception. here it is:

F:\TMN\insertlogdb>echo %classpath%
C:\jdk\lib\classes12.zip;C:\tomcat5\common\lib\servlet.jar;c:\jdk\bin;c:\jdk;C:\
jdk\lib\tools.jar;C:\tomcat5\webapps\TMN\WEB-INF\lib\ojdbc14.jar;f:\tmn\junk\

F:\TMN\insertlogdb>set classpath=%CLASSPATH%;f:\TMN\insertlogdb\insertLogDb.jar

F:\TMN\insertlogdb>echo %classpath%
C:\jdk\lib\classes12.zip;C:\tomcat5\common\lib\servlet.jar;c:\jdk\bin;c:\jdk;C:\
jdk\lib\tools.jar;C:\tomcat5\webapps\TMN\WEB-INF\lib\ojdbc14.jar;f:\tmn\junk\;f:
\TMN\insertlogdb\insertLogDb.jar

F:\TMN\insertlogdb>java com.oberon.runtime.ZipFileRuntime insertLogDb.jar  

java.lang.UnsatisfiedLinkError: no prtjt42 in java.library.path
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at com.oberon.runtime.Utilities.<clinit>(Unknown Source)
        at com.oberon.runtime.NativeValue.<clinit>(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.oberon.runtime.ProsperoRuntime.setValueClass(Unknown Source)
        at com.oberon.runtime.ProsperoRuntime.<init>(Unknown Source)
        at com.oberon.runtime.ProsperoRuntime.getInstance(Unknown Source)
        at com.oberon.runtime.ZipFileRuntime.main(Unknown Source)

Whats this error ??
thnx
0
 
objectsCommented:
it saying it cannot find a required dll
0
 
CEHJCommented:
>>Whats this error ??

That's probably because you've moved things around. Better get back to the previous example. Don't panic. Unnecessary changes can cause *extra* problems
0
 
objectsCommented:
> Ok, I recompiled a new jar  and placed it in a different directory and when running it got a new exception

Sounds like there was a problem with the build of the previous jar
0
 
CEHJCommented:
>>@CEHJ - yes, jar -tvf list the classes I need in insertDB.jar

Meaning ZipFileRuntime is in there?
0
 
sands76Author Commented:
OK! finally.. its working.. I had not place a DLL in the dir..

CEHJ, your suggestion worked! that of setting the classpath in the batch file.

Thnks to everyone.

btw, one last question. I would be executing this jar as a CRON on a Unix machine. Do I follow the same procedure as that of the  batch file or is it any different ??

thnx,
0
 
CEHJCommented:
:-)

>>Do I follow the same procedure as that of the  batch file or is it any different ??

Same procedure essentially
0
 
objectsCommented:
Glad we could help :)

http://www.objects.com.au/staff/mick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.