This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Running java with a classpath on cygwin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running java with a classpath on cygwin" Watch "Running java with a classpath on cygwin" New topic
Author

Running java with a classpath on cygwin

John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Hi folks,

I'm trying to run a simple example using my own built Jar file which requires the spring framework Jar on the classpath. I am doing this with JSE 6, Spring Framework 2.5.1 and cygwin 1.7.5.

When I attempt to do this I get the following error message;



So in order to isolate the problem I created a simple example where I create two separate Jars with the second depending on the first;

com.mcparland.john.dependon.DependOnMe


com.mcparland.john.dependent;


Both are compiled and packaged into their own Jar files, DependOn.jar and Dependent.jar. Clearly Dependent needs to be built with DependOnMe on the classpath. Dependent.jar has a Main-Class in the manifest, com.mcparland.john.dependent.Dependent.

Then I run as follows and get the same error;


I cannot see the problem but I imagine it might have something to do with the use of -cp. I do not have a CLASSPATH set.

Many thanks,

John

PS

In the small sample above both package hierarchies exist in /home/John/testJarDependencies/<projectName>/bin where project names are dependOn and dependent.


Passed: SCJP 6 (90%), SCJD 6
Other: Spring training, extensive Swing experience
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Not sure if you can use cygpath there. Why don't you just try it like this and see if it works.



SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Thanks Freddy,

unfortunately I tried that but with no luck. It's a bit confusing since I believe the java command is looking for windows paths but I'm doing this in a cygwin environment.

I tried the following;



And each time received the same errors. The reason for each different type is;

1. /cydrive/... is the absolute path as per cygwin's view of the file system
2. /Spring/... is the absolute path when looking at it from a Windows point of view (well using different separators and ommitting the C). This is the format used in the Ant build file I used.

Many thanks,

John
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

I think I can rule out cygwin as the culprit as the same thing happens in DOS;





I have to say this is driving me mad
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

The -cp flag is ignored when using -jar. Instead you must add the Spring JAR file in the Class-Path entry of your manifest file. And no, that does not like absolute paths much, I can already tell you that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Thanks Rob,

using just -cp and naming the class at the end of the Java command enabled it to resolve the problem with the Spring library but it still cannot find the main class;



When I check the contents of Hello.jar;



I can see com.springinaction.chapter01.hello.HelloApp is there but I cannot understand why it cannot be found.

Also why do I need the Spring Jar in the Class-Path of the manifest file? I needed it to build the Jar obviously but I don't see why it is needed in the manifest?

Many thanks,

John
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You do need it in your JAR file, or the HelloApp class cannot run.
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Sorry I'm not sure I understand why I need to put the class path in the Jar. The main reason why is that if I do not package HelloApp into a Jar file and instead run it from the directory containing the class hierarchy I still get the same error;



John
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

What happens if you use : as the separator? Although you're using Cygwin, I think that the java executable is a Windows one. The class path is not split by the shell but by the JVM, and a Windows JVM will try to split at ;.
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

If I use the ; as the separator I get the Java usage information together with these errors (since it is trying to run Hello.jar);

John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Right added an extra Jar to the classpath and tried on Windows and it worked!




However using the same classpath on cygwin still gives me the problem that it cannot find the main class;



Any ideas?
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Finally got it!



cypath is needed because Java is a windows program;
http://www.inonit.com/cygwin/faq/

spring.jar and commons-logging.jar contains the classes necessary for development with spring.

Hello.jar contains the classes I developed.

com.springinaction.chapter01.hello.HelloApp is the main class.

I didn't have to specify anything in the manifest and the only extra thing I did in my ant build file was to add the spring dependancies to the build classpath.

Thanks to all who posted.
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
I'm struggling with this same thing. I tried the suggestion of using:

but i get an error saying the cygpath command is not found.
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
worked for me now. not sure why though, anyway......
marcelo rojas
Greenhorn

Joined: Jan 27, 2011
Posts: 3
John McParland wrote:Finally got it!



cypath is needed because Java is a windows program;
http://www.inonit.com/cygwin/faq/

spring.jar and commons-logging.jar contains the classes necessary for development with spring.

Hello.jar contains the classes I developed.

com.springinaction.chapter01.hello.HelloApp is the main class.

I didn't have to specify anything in the manifest and the only extra thing I did in my ant build file was to add the spring dependancies to the build classpath.

Thanks to all who posted.


Thank you very much for your post! I really helped me too!

cheers!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running java with a classpath on cygwin
 
Similar Threads
Ant - How to create a runnable jar of jars and classes
java unable to recognize class where my IDE does
running a .class file from inside a jar
Web Service with build.xml
error