Solved

Executing a jar via batch

Posted on 2004-08-13
28
1,395 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:sands76
  • 10
  • 9
  • 7
  • +2
28 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11793363
If executable jar

java -jar <jarfile name> <fully-qualified main classname>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11793368
Sorry

Executable jar:

java -jar <jarfile name>

Non-executable:

java -classpath <jarfile name> <fully-qualified main classname>
0
 
LVL 1

Author Comment

by:sands76
ID: 11793602
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11793674
Sorry - losing concentration ;-)

Simply put the same command you're using successfully into the batch file
0
 
LVL 1

Author Comment

by:sands76
ID: 11793821
that was the first thing I tried.. its not working :(

argh. i hate classpaths!
0
 
LVL 1

Expert Comment

by:zeus_akkali
ID: 11794552
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
 
LVL 1

Author Comment

by:sands76
ID: 11794914
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 11795971
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
 
LVL 92

Expert Comment

by:objects
ID: 11797890
How are you running your batch, any other command preceding the java call
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 11799403
>>
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
 
LVL 1

Author Comment

by:sands76
ID: 11808400
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
 
LVL 92

Expert Comment

by:objects
ID: 11808405
what command did you use
0
 
LVL 92

Expert Comment

by:objects
ID: 11808410
should be:

java <classname>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808422
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11808427
>>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
 
LVL 92

Expert Comment

by:objects
ID: 11808436
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
 
LVL 92

Expert Comment

by:objects
ID: 11808444
what exactly is the exception you are now getting?
0
 
LVL 1

Author Comment

by:sands76
ID: 11808465
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808478
>>com.oberon.runtime.ZipFileRuntime

What jar is that class in?

Can you prove it by

jar -tvf someJar.jar

?
0
 
LVL 92

Expert Comment

by:objects
ID: 11808485
try:

java com.oberon.runtime.ZipFileRuntime
0
 
LVL 1

Author Comment

by:sands76
ID: 11808644
@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
 
LVL 92

Expert Comment

by:objects
ID: 11808673
it saying it cannot find a required dll
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808677
>>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
 
LVL 92

Expert Comment

by:objects
ID: 11808678
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808680
>>@CEHJ - yes, jar -tvf list the classes I need in insertDB.jar

Meaning ZipFileRuntime is in there?
0
 
LVL 1

Author Comment

by:sands76
ID: 11808687
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808703
:-)

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

Same procedure essentially
0
 
LVL 92

Expert Comment

by:objects
ID: 11808734
Glad we could help :)

http://www.objects.com.au/staff/mick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 59
Java array passed to SQL where clause 23 62
java jdbc batch example 8 20
java stored proc example 9 20
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now