Solved

Executing a jar via batch

Posted on 2004-08-13
28
1,389 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
Comment Utility
If executable jar

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

Expert Comment

by:CEHJ
Comment Utility
Sorry

Executable jar:

java -jar <jarfile name>

Non-executable:

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

Author Comment

by:sands76
Comment Utility
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
Comment Utility
Sorry - losing concentration ;-)

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

Author Comment

by:sands76
Comment Utility
that was the first thing I tried.. its not working :(

argh. i hate classpaths!
0
 
LVL 1

Expert Comment

by:zeus_akkali
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How are you running your batch, any other command preceding the java call
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
>>
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
Comment Utility
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
Comment Utility
what command did you use
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
should be:

java <classname>
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
what exactly is the exception you are now getting?
0
 
LVL 1

Author Comment

by:sands76
Comment Utility
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
Comment Utility
>>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
Comment Utility
try:

java com.oberon.runtime.ZipFileRuntime
0
 
LVL 1

Author Comment

by:sands76
Comment Utility
@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
Comment Utility
it saying it cannot find a required dll
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
> 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
Comment Utility
>>@CEHJ - yes, jar -tvf list the classes I need in insertDB.jar

Meaning ZipFileRuntime is in there?
0
 
LVL 1

Author Comment

by:sands76
Comment Utility
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
Comment Utility
:-)

>>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
Comment Utility
Glad we could help :)

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

6 Experts available now in Live!

Get 1:1 Help Now