• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Cheers guys!
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables in batch files are denoted by percent signs, so the correct syntax would be

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!