Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Difference between specifying classpath with -cp in command line and in Eclipse-> Run Configurations

Hi,
I ran a program by giving the following command, the program ran successfully :
java -cp "lib/*;classes" com.yatra.scheduler.runner.MOScheduleSuperPnr

I tried running the same thing from Eclipse. By selecting Run Configurations -> Classpath and then adding all the jars inside lib directory under the User Entries Heading and also the classes folder.

But this gives a runtime error Classdefnotfound.

What could be the reason for this ?
Whats the difference between specifying the classpath in command line using the -cp option and specifying classpath in Eclipse -> Run Configurations->Classpath

Thanks
Avatar of mccarl
mccarl
Flag of Australia image

Whats the difference between specifying the classpath in command line using the -cp option and specifying classpath in Eclipse -> Run Configurations->Classpath
There should be no difference. Can you post a screenshot of the "Classpath" page from the Run Configurations dialog?
What could be the reason for this ?

Possibles:

a. you missed at least one jar
b. the lib folder also includes classes NOT in jars
Avatar of Rohit Bajaj

ASKER

HI,
I think i spotted the problem. Any comments on improving will be helpful.

For the normal run of the code i am running the following command :

java -cp "lib/*;classes" com.yatra.scheduler.runner.MOScheduleSuperPnr

under the directory E:\SvnCheckOut\trunk\MOScheduler\target\MOScheduler


But in eclipse i directly click on run under the file

/MOScheduler/src/com/yatra/scheduler/runner/MOScheduleSuperPnr.java

And so it gives the following error :
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'classes/velocity/flight_booking.vm'

Now during command prompt running this error was not coming.
Due to the fact that classes folder was present under E:\SvnCheckOut\trunk\MOScheduler\target\MOScheduler

How do i correct this or whats the proper way to run it in eclipse. I need to debug it in eclipse.

Thanks
Sounds like you have not set the Eclipse project up properly. You'd probably be better off creating a new project and making sure you have got the build path and packaging correct.

That is, of course if it's not already an Eclipse project. If it is, it should contain a .project file
Ok, so the problem is that you are trying to load your velocity resource as a file via a relative path. So the working directory that current when you first launch the app is critical to it working.

Quick and dirty solution... Go into your Run Configuration in Eclipse and then on either the Arguments tab or Environment tab (I forget exactly which one), there is a field to be able to set the working directory. Set this to the same directory that you are in on the command line and then it should work from eclipse too.

However what you should do is have Velocity load the .vm file via the classpath rather than as a file. First check out Velocity's ClasspathResouceLoader, but for exactly how to enable this, you would have to tell us exactly how you are using Velocity, ie. do you manually instantiate a VelocityEngine from code, or do you use some integration with another technology such as Spring, etc?
Yes, certainly it's better to load your templates as resources:

http://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse

A natural way (close to the Java standard one is)

Resource r = org.apache.velocity.util.ClassUtils.getResourceAsStream(getClass(), "/resources/flight_booking.vm");

Open in new window

Hi,
I am calling velocity at the line :
Template velocityTemplate = velocityEngine.getTemplate(vmFilePath);
Here vmFilePath = classes/velocity/flight_booking.vm

Its failing at this point.

I tried changing the working directory in Eclipse argument tab. It worked perfectly.
Please also let me know the other approach.
Which is the better one to use ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial