• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

NoClassDefFoundError when running a java app from outside it's folder

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a simple java app.
I have prepared a batch file (myBatch.bat) to I run it like this:
C:\j2sdk1.4.2_15\bin\java.exe com.my.app.Generator

The batch file is located in the app's folder.
When I double click the batch file it's working fine but when I try to run the batch file from command line with a different location, for example:
c:\someFolder>C:\myApp>myBatch.bat

I get: Exception in thread "main" java.lang.NoClassDefFoundError.

What needs to be changed in order for it to run from any place?
[ October 31, 2007: Message edited by: Roy Cohen ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set the classpath so that Java can find your class files: add the "-classpath" option in your batch file:

C:\j2sdk1.4.2_15\bin\java.exe -classpath C:\myApp com.my.app.Generator
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here is my batch file:
C:\j2sdk1.4.2_15\bin\java.exe -classpath lib/ojdbc14.jar;lib/activation.jar;lib/mail.jar; com.my.app.RunGenerator

lib is a folder in the project's folder.
The project code and the batch file is located under c:\workspace\DeletionGenerator\

Can you please show me on these lines what needs to be changed if I run it from c:> ?
[ October 31, 2007: Message edited by: Roy Cohen ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the current directory is not c:\workspace\DeletionGenerator\ java will not find the jars you put in the lib sub-directory. If you want your batch file to work in any directory, you need to put the whole path for each jar file in your classpath:


C:\j2sdk1.4.2_15\bin\java.exe -classpath c:\workspace\DeletionGenerator\lib\ojdbc14.jar;c:\workspace\DeletionGenerator\lib\activation.jar;c:\workspace\DeletionGenerator\lib\mail.jar; com.my.app.RunGenerator
 
Sheriff
Posts: 22700
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To prevent typos, or simplify moving the entire folder, it's better to use a variable inside the batch file:

(enters added for clarity)

It's best to put quotes around the path, so if you move it to a path with spaces in it it will still work.
[ October 31, 2007: Message edited by: Rob Prime ]
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers guys!
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I'm bringing this post back to life because still got problems with it.
If I do what you suggest I still get NoClassDefFoundError on the main class, i.e. RunGenerator if I run it from outside the folder.

Any ideas?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables in batch files are denoted by percent signs, so the correct syntax would be

 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic