Possible using jar Libraries?

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Possible using jar Libraries?

Postby saturn_at_sky » Wed Jan 22, 2014 12:15 pm

Is it possible by now, to use jar libraries? Only uploading a library does not work. What I have to do?
User avatar
saturn_at_sky
New User
 
Posts: 21
Joined: Wed Jan 08, 2014 1:55 pm
Location: Germany

Re: Possible using jar Libraries?

Postby gloomyandy » Wed Jan 22, 2014 12:25 pm

Yes you can use jars, however you will need to get them on to the EV3 and you will need to include them in the classpath in some way. The current menu system may not provide mechanisms to do all of this (to be honest I'm not sure, Lawrie will have a better idea), but you can certainly do it using ssh and other command line tools. You may also be able ton include the jars you want to use into your programs jar.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3632
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Possible using jar Libraries?

Postby lawrie » Wed Jan 22, 2014 10:44 pm

Hi saturn_at_sky,

I am not sure what you are asking. The menu only runs runnable jar files. These define the main program to call and specify the class path. If you want the runnable jar file to use other library jar files, that is fine. You just need to upload them to the EV3 and reference them in the class path in the manifest of your runnable jar file. This can all be done in ant build files.

What are you trying to do, and what is wrong with a single runnable jar file?

Lawrie
lawrie
leJOS Team Member
 
Posts: 836
Joined: Mon Feb 05, 2007 1:27 pm

Re: Possible using jar Libraries?

Postby saturn_at_sky » Fri Jan 24, 2014 9:37 am

gloomyandy and Lawrie, thank you for your hints.
I don't know much about ant and so I developed the following procedure. I transfer the classes of the jar library into the jar file of my main program. For this, the ant file of my project must be expanded. This is my new build.xml:

Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="TicTacToe" basedir="." default="copy">
    <property name="source.dir"     value="src"/>
    <property name="lib.dir"        value="C:/Users/Thomas/git/ev3/ev3classes"/>
    <property name="extlib.dir"     value="C:/_Thomas/Roboter/EV3/_Java/Eclipse/EV3Lib/dist"/>
    <property name="class.dir"      value="build"/>
    <property name="jar.dir"        value="dist"/>
    <property name="main-class"     value="TicTacToe"/>
    <property name="lejos.home"     value="/home/root/lejos"/>
    <property name="ev3.host"       value="192.168.178.21"/>

    <path id="libraries.path">   
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${extlib.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>

   <ant dir="C:/_Thomas/Roboter/EV3/_Java/Eclipse/EV3Lib"/>

    <target name="clean" description="delete old files">
        <delete dir="${class.dir}"/>
        <delete dir="${jar.dir}"/>
    </target>

    <target name="compile" description="build class files" depends="clean">
        <mkdir dir="${class.dir}"/>
        <javac srcdir="${source.dir}" destdir="${class.dir}">
            <classpath refid="libraries.path"/>
        </javac>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
                <attribute name="Class-Path" value="${lejos.home}/lib/ev3classes.jar ${lejos.home}/libjna/usr/share/java/jna.jar"/>
            </manifest>
        </jar> 
    </target>

    <target name="extjar" depends="jar">
     <exec dir="." executable="cmd.exe">
       <arg line="/c call MakeJar.bat"/>
     </exec>
    </target>

    <target name="copy" depends="extjar">
        <scp todir="root:@${ev3.host}:/home/lejos/programs/" trust="true">
            <fileset file="${jar.dir}/${ant.project.name}.jar"/>
        </scp>
    </target>
</project>


First I put the path of my library into the class path. Next I start the build.xml of my Library. Before the copy I start the MakeJar.bat. This is the coding:

Code: Select all
echo off
rem ********************************************
rem * make jar for the TicTacToe project           *
rem * (c) 2014 Thomas Kaffka, Cologne, Germany *
rem ********************************************

cd C:\_Thomas\Roboter\EV3\_Java\Eclipse\EV3Lib\build
copy C:\_Thomas\Roboter\EV3\_Java\Eclipse\TicTacToe\dist\TicTacToe.jar *.*

echo make Jar-File
C:\Programme\Java\jdk1.6.0_23\bin\jar -ufv TicTacToe.jar *.class tool

copy TicTacToe.jar C:\_Thomas\Roboter\EV3\_Java\Eclipse\TicTacToe\dist\*.*

echo until now everything ok :-)


This bat file copies the jar of my main program into the build folder of the library. Than the jar-program adds the found classes to the jar file of my main program. After that it copies the jar file back to its dist folder. This is not a very elegant solution, but it works.
User avatar
saturn_at_sky
New User
 
Posts: 21
Joined: Wed Jan 08, 2014 1:55 pm
Location: Germany

Re: Possible using jar Libraries?

Postby alex445 » Sat Jan 25, 2014 11:24 am

<include name="*.jar"/>
</fileset>
<fileset dir="${extlib.dir}">
<include name="*.jar"/>
</fileset>
</path>

<ant dir="C:/_alex/config/EV3/_Java/Eclipse/EV3Lib"/>

<target name="clean" description="delete old files">
<delete dir="${class.dir}"/>
<delete dir="${jar.dir}"/>
</target>










i have problem in this code ..............
can anybody correct it?????????????
Mani
alex445
New User
 
Posts: 1
Joined: Sat Jan 25, 2014 11:16 am

Re: Possible using jar Libraries?

Postby saturn_at_sky » Sat Jan 25, 2014 5:00 pm

Hi Alex,
it is important, that you use the right paths of your Computer system. And, this method works only on a Windows os.

<property name="extlib.dir" value="C:/_Thomas/Roboter/EV3/_Java/Eclipse/EV3Lib/dist"/>
Here you must use the path to your Library jar file.

<ant dir="C:/_Thomas/Roboter/EV3/_Java/Eclipse/EV3Lib"/>
Here you must use the path of your Library Folder. It is important that there is a build.xml which builds the library. Delete the scp-tag in it, because it is not necessary to copy the library to the EV3.

good luck...
User avatar
saturn_at_sky
New User
 
Posts: 21
Joined: Wed Jan 08, 2014 1:55 pm
Location: Germany

Re: Possible using jar Libraries?

Postby saturn_at_sky » Sun Jan 26, 2014 10:12 am

I found a better solution based on Lawries hint.

1) Upload the Library (EV3Lib.jar) to the EV3.

2) Insert the path of the library into the class path to compile the main program:
Code: Select all
<property name="extlib.dir"     value="C:/_Thomas/Roboter/EV3/_Java/Eclipse/EV3Lib/dist"/>

<fileset dir="${extlib.dir}">
   <include name="*.jar"/>
</fileset>


3) Insert the path of the library in the EV3 into the class path of the manifest:
Code: Select all
<attribute name="Class-Path" value="${lejos.home}/lib/ev3classes.jar ${lejos.home}/libjna/usr/share/java/jna.jar /home/lejos/programs/EV3Lib.jar"/>


4) Upload the main program with this ant script:

Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="TicTacToe" basedir="." default="copy">
    <property name="source.dir"     value="src"/>
    <property name="lib.dir"        value="C:/Users/Thomas/git/ev3/ev3classes"/>
    <property name="extlib.dir"     value="C:/_Thomas/Roboter/EV3/_Java/Eclipse/EV3Lib/dist"/>
    <property name="class.dir"      value="build"/>
    <property name="jar.dir"        value="dist"/>
    <property name="main-class"     value="n4e.tictactoe.system.TicTacToe"/>
    <property name="lejos.home"     value="/home/root/lejos"/>
    <property name="ev3.host"       value="192.168.178.21"/>

    <path id="libraries.path">   
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${extlib.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="clean" description="delete old files">
        <delete dir="${class.dir}"/>
        <delete dir="${jar.dir}"/>
    </target>

    <target name="compile" description="build class files" depends="clean">
        <mkdir dir="${class.dir}"/>
        <javac srcdir="${source.dir}" destdir="${class.dir}">
            <classpath refid="libraries.path"/>
        </javac>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
                <attribute name="Class-Path" value="${lejos.home}/lib/ev3classes.jar ${lejos.home}/libjna/usr/share/java/jna.jar /home/lejos/programs/EV3Lib.jar"/>
            </manifest>
        </jar> 
    </target>

      <target name="copy" depends="jar">
        <scp todir="root:@${ev3.host}:/home/lejos/programs/" trust="true">
            <fileset file="${jar.dir}/${ant.project.name}.jar"/>
        </scp>
    </target>
</project>
User avatar
saturn_at_sky
New User
 
Posts: 21
Joined: Wed Jan 08, 2014 1:55 pm
Location: Germany

Re: Possible using jar Libraries?

Postby rafalmag » Fri Feb 14, 2014 11:40 pm

I recommend using maven for libraries dependency management and build.

How to do it? :
1. In Eclipse convert DBUSJava into maven project.
2. Add jna dependency (as maven is handling dependencies in its repository)
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.2.7</version>
</dependency>
3. do "maven install" on DBUSJava (to add it your local maven repository)
4. convert ev3classes into maven project
5. add dependency to DBUSJava project (so it could use DBUsJava classes)
6. do "maven install" on ev3classes (to add it your local maven repository)
7. Create your own maven project - more details in comments / in my example pom.xml below
8. do "maven install" on your project - it will automatically deploy it to your EV3 device.

My project's pom.xml:
Code: Select all
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>EV3Clock</groupId>
   <artifactId>EV3Clock</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <properties>
      <brick.host>10.0.1.1</brick.host>
      <brick.home>/home/lejos/programs</brick.home>
      <brick.user>root</brick.user>
   </properties>
   <dependencies>
      <dependency>
         <groupId>lejos</groupId>
         <artifactId>ev3classes</artifactId>
         <version>0.6.0-SNAPSHOT</version>
         <!-- scope provided means that it will be used for compilation, but you have to add it to classpath in order to use it during runtime -->
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>commons-net</groupId>
         <artifactId>commons-net</artifactId>
         <version>3.3</version>
      </dependency>
      <dependency>
         <groupId>ch.qos.logback</groupId>
         <artifactId>logback-classic</artifactId>
         <version>1.0.13</version>
      </dependency>
      <dependency>
         <groupId>com.google.guava</groupId>
         <artifactId>guava</artifactId>
         <version>15.0</version>
      </dependency>
      
      <!-- tests only -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.assertj</groupId>
         <artifactId>assertj-core</artifactId>
         <version>1.5.0</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-all</artifactId>
         <version>1.3</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
         <version>1.9.5</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>pl.pragmatists</groupId>
         <artifactId>JUnitParams</artifactId>
         <version>1.0.2</version>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <!-- to point our java 7 compatibility -->
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.7</source>
               <target>1.7</target>
            </configuration>
         </plugin>
         <!-- build a runnable jar with dependencies -->
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>pl.rafalmag.ev3.clock.MainWithMenu</mainClass>
                  </manifest>
                  <manifestEntries>
                  <!-- if ev3classes scope is "provided" -> make sure this point to existing files-->
                     <Class-Path>/home/root/lejos/lib/ev3classes.jar
                        /home/root/lejos/lib/dbusjava.jar
                        /home/root/lejos/libjna/usr/share/java/jna.jar</Class-Path>
                  </manifestEntries>
               </archive>
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id>
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <!-- deploy jar with dependencies on EV3 -->
         <plugin>
            <inherited>false</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
               <execution>
                  <phase>install</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <target>
                        <echo
                           message="Push to server ${project.build.directory}/${project.build.finalName}*.jar">
                        </echo>
                        <scp trust="yes" username="${brick.user}" password=""
                           todir="${brick.user}@${brick.host}:${brick.home}">
                           <fileset dir="${project.build.directory}">
                              <include name="${project.build.finalName}*dependencies.jar">
                              </include>
                           </fileset>
                        </scp>
                     </target>
                  </configuration>
               </execution>
            </executions>
            <dependencies>
               <!-- ANT dependencies for this plugin - mainly to be able to use SCP -->
               <dependency>
                  <groupId>commons-net</groupId>
                  <artifactId>commons-net</artifactId>
                  <version>1.4.1</version>
               </dependency>
               <dependency>
                  <groupId>ant</groupId>
                  <artifactId>ant-commons-net</artifactId>
                  <version>1.6.5</version>
               </dependency>
               <dependency>
                  <groupId>ant</groupId>
                  <artifactId>ant-jsch</artifactId>
                  <version>1.6.5</version>
               </dependency>
               <dependency>
                  <groupId>jsch</groupId>
                  <artifactId>jsch</artifactId>
                  <version>0.1.29</version>
               </dependency>
               <dependency>
                  <groupId>ant-contrib</groupId>
                  <artifactId>ant-contrib</artifactId>
                  <version>20020829</version>
               </dependency>
            </dependencies>
         </plugin>
      </plugins>
   </build>

</project>


Just for reference:
My DBusJava pom.xml:
Code: Select all
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>lejos</groupId>
  <artifactId>DBusJava</artifactId>
  <version>0.6.0-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
     <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>3.2.7</version>
     </dependency>
  </dependencies>
</project>


My ev3classes pom.xml:
Code: Select all
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>lejos</groupId>
  <artifactId>ev3classes</artifactId>
  <version>0.6.0-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
     <dependency>
        <groupId>lejos</groupId>
        <artifactId>DBusJava</artifactId>
        <version>0.6.0-SNAPSHOT</version>
     </dependency>
  </dependencies>
</project>
rafalmag
New User
 
Posts: 3
Joined: Mon Feb 10, 2014 11:40 pm

Re: Possible using jar Libraries?

Postby tigger » Sat Feb 15, 2014 12:45 am

@saturn_at_sky

On the EV3 i prefer the place /home/root/lejos/lib/ (in manifest task ${lejos.home}/lib/) for my own library right next to ev3classes.jar. So /home/lejos/programs contains only executable JAR to avoid confusing myself :)

To find out my preferred target location on EV3 - /home/root/lejos/lib/ - it was very helpful to connect my EV3 [url:http://sourceforge.net/p/lejos/wiki/Connecting%20to%20the%20EV3%20with%20ssh/]as described in Wiki[/url], the linux accepts "cd", "pwd", "ls" and other unix commands as expected. First I was confused by /home/root vs. /home/lejos, but the separation is helpful to hide "base" libraries from menu so nobody is trying to start a lib without a main method or delete it from stick by accident.

@rafalmag:

Maven is a powerful tool but hard to understand for a beginner. For hobbyists like me with only a single own library, without a release management and without dependency trees ant is my preferred way. At work with a lot of independent modules, with 3rd party libraries, unit- and integration tests, continuous integration system and a nexus repo Maven is neccessary, but at home I don't need all these Maven features so I'm fine by using ant.
tigger
New User
 
Posts: 17
Joined: Mon Feb 10, 2014 10:04 pm

Re: Possible using jar Libraries?

Postby daspfanni2000 » Sat Feb 15, 2014 9:20 pm

Edit:

I still have a problem, but this is what I found out so far.

To copy a library jar file to ev3, you drag and drop it into your project.

Then edit build.xml
Code: Select all
<target name="copy" depends="jar">
        <scp todir="root:@${ev3.host}:/home/lejos/programs/" trust="true">
            <fileset file="${jar.dir}/${ant.project.name}.jar"/>
        </scp>
        <scp todir="root:@${ev3.host}:${lejos.home}/lib/" trust="true">
            <fileset file="svgSalamander.jar"/>
        </scp>


This works just fine.

But then I am struggling. I edited the class path of the build.xml:
Code: Select all
    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
                <attribute name="Class-Path" value="${lejos.home}/lib/ev3classes.jar ${lejos.home}/libjna/usr/share/java/jna.jar ${lejos.home}/lib/svgSalamander.jar"/>
            </manifest>
        </jar> 
    </target>


In Eclipse I edited the project settings to use the jar file that is locally in the project folder. (Includes seem to be OK)

When executing the ant, I still get the following errors:
Code: Select all
    [javac] /Users/myname/Developer/workspace/TRAC3R/src/SVGHandler.java:1: error: package com.kitfox.svg does not exist
    [javac] import com.kitfox.svg.*;
    [javac] ^
    [javac] 1 error


How can I fix this?!

EDIT again:
This was missing, right?
Code: Select all
    <path id="libraries.path">   
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir=".">
            <include name="svgSalamander.jar"/>
        </fileset>
    </path>
daspfanni2000
New User
 
Posts: 19
Joined: Sun Dec 22, 2013 5:34 pm


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff