# Rosjava gradle build java classpath

Hi,

I've spent the last week learning gradle with little success. It seems overly complex and unnecessary for rosjava, but I will try to put my complaints aside. No matter what I do, after a gradle build (either using gradlew installApp or gradle build), my java class path is not set. I check my class path with  echo \$CLASSPATH  and it always spits out a blank line. I even tried this from the pubsub tutorial and got the same result. This leads me to believe that it is normal behavior. However, if this is the case, how do you include dependencies? I follow the gradle default set up with my source code in main/src/main/java and my external .jar in src/main/resources, which gradle seems to use as the default (and I specify it in the gradle build script anyway for redundancy). Could someone please help?

src located in: /src/main/java/org/ros/rosd where org/ros/rosd is the package.

.jar in: /src/main/resources/d.jar

I know the jar runs fine, and after building I can manually run the java code:  roscore & ./build/install/rosd/bin/rosd org.ros.rosd.talker & ./build/install/rosd/bin/rosd org.ros.rosd.listener & 

However, whenever I try to initialize a constructor from something in the .jar, it fails to recognize it as an actual resource. For example, adding the following to talker.java:  new ds();  throws a compile-time error. Though, I can manually run the jar with java -jar d.jar

edit: I should note that within talker.java I import edu.wpi.d; and it doesn't find the package, which is in d.jar.

Your help would be greatly appreciated!

--James

edit retag close merge delete

Sort by » oldest newest most voted

I've solved this problem. If you want to define an external jar file located on your own system within gradle, it's as simple as adding:

 compile files('libs/d.jar') 

to the dependencies. So your dependencies look something like:

 dependencies { compile 'ros.rosjava_core:rosjava:0.0.0-SNAPSHOT' compile files('libs/d.jar') } 

An entire week on that tiny bit. At least I understand gradle dependencies quite well now.

Cheers, JAmes

more

Thanks James. I'm going to use this to add the slf4j logger to my classpath. I'm having some trouble building the rosjava build.

( 2013-03-24 23:47:57 -0500 )edit

i created another folder called lib in my package and put my jar in that lib folder. I aded this linein my build.gradle "compile files('lib/findtask-ros-1.0.0-SNAPSHOT.jar') " and so my dependencies looks like this "dependencies { compile project(':rosjava') compile files('lib/findtask-ros-1.0.0-SNAPSHOT.jar') } Is this all I need to do? Do I also need to include an import statement for the packages in this jar in my code?

( 2014-01-20 14:06:26 -0500 )edit

Using wildcards in java classpath

Wild cards were introduced from Java 6. Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For ex. a classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

Example

java -cp "lib/*" %MAINCLASS% where %MAINCLASS% is the class containing your main method.

How to use a wildcard in the classpath to add multiple jars

java -cp "lib/*" -jar %MAINJAR%

where %MAINJAR% is the jar file to launch via its internal manifest.

If you need only specific jars, you will need to add them individually. The classpath string does not accept generic wildcards like Jar, *.jar, hiber etc.

Example

The following entry does not work:

java -cp "Halo.jar;lib/*.jar" ni.package.MainClass

Correct entry is :

java -cp "Halo.jar;lib/*" ni.package.MainClass

Ling

more