[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Executing a jar via batch

Posted on 2004-08-13
28
Medium Priority
?
1,432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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
 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

656 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