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

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
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?
Top Expert 2016

Commented:
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

Author

Commented:
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
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Top Expert 2016

Commented:
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
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
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?
Top Expert 2016

Commented:
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

Author

Commented:
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
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
If you are using a velocity.properties file, then add/change the following lines...
resource.loader = classpath
classpath.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

Open in new window

Otherwise, if you are NOT using the properties file, you need to set the properties in your code, using something like the below, before velocityEngine.init()...
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

Open in new window

With either of the above, you would only specify "velocity/flight_booking.vm" as the vmFilePath value, ie. drop the "classes/" because that directory is already on your classpath.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial