antiotsudoerscoojacontiki-ng

How can I build with ant as a normal user (no need for sudo) COOJA



Recently I started working on a project in IoT, using contiki-ng.
The problem I faced is when I tried to run cooja with:
$ ant run

under ~/contiki-ng/tools/cooja. the build failed

$ ant run
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
Buildfile: /home/user/contiki-ng/tools/cooja/build.xml

init:

compile:

copy configs:

jar_cooja:
     [jar] Building jar: /home/user/contiki-ng/tools/cooja/dist/cooja.jar

jar:

init:

compile:

jar:

init:

mspsim:

init:

compile:

jar:

coffee:

init:

compile:
   [javac] Compiling 8 source files to /home/user/contiki-ng/tools/cooja/tools/coffee-manager/build
   [javac] Note: /home/user/contiki-ng/tools/cooja/tools/coffee-manager/org/contikios/coffee/CoffeeConfiguration.java uses or overrides a deprecated API.
   [javac] Note: Recompile with -Xlint:deprecation for details.

configs:

jar:
     [jar] Building jar: /home/user/contiki-ng/tools/cooja/tools/coffee-manager/coffee.jar
    [copy] Copying 1 file to /home/user/contiki-ng/tools/cooja/apps/mspsim/lib

compile:

copy:

jar:

init:

compile:

jar:

init:

clean:
  [delete] Deleting directory /home/user/contiki-ng/tools/cooja/apps/serial_socket/build

BUILD FAILED
/home/user/contiki-ng/tools/cooja/build.xml:200: The following error occurred while executing this line:
/home/user/contiki-ng/tools/cooja/apps/serial_socket/build.xml:23: Unable to delete file /home/user/contiki-ng/tools/cooja/apps/serial_socket/build/org/contikios/cooja/serialsocket/SerialSocketClient$5.class

Total time: 2 seconds

I tried using $ sudo ant run , and it started working as it should, without a problem.
.
.
$ ant run -verbose 2>> log;cat log gives:

Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true

BUILD FAILED
/home/user/contiki-ng/tools/cooja/build.xml:200: The following error occurred while executing this line:
/home/user/contiki-ng/tools/cooja/apps/serial_socket/build.xml:23: Unable to delete file /home/user/contiki-ng/tools/cooja/apps/serial_socket/build/org/contikios/cooja/serialsocket/SerialSocketClient$5.class
    at org.apache.tools.ant.taskdefs.Delete.handle(Delete.java:775)
    at org.apache.tools.ant.taskdefs.Delete.removeDir(Delete.java:822)
    at org.apache.tools.ant.taskdefs.Delete.removeDir(Delete.java:818)
    at org.apache.tools.ant.taskdefs.Delete.removeDir(Delete.java:818)
    at org.apache.tools.ant.taskdefs.Delete.removeDir(Delete.java:818)
    at org.apache.tools.ant.taskdefs.Delete.removeDir(Delete.java:818)
    at org.apache.tools.ant.taskdefs.Delete.execute(Delete.java:641)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:299)
    at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99)
    at org.apache.tools.ant.Task.perform(Task.java:350)
    at org.apache.tools.ant.Target.execute(Target.java:449)
    at org.apache.tools.ant.Target.performTasks(Target.java:470)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1401)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:36)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1264)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:437)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:299)
    at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99)
    at org.apache.tools.ant.Task.perform(Task.java:350)
    at org.apache.tools.ant.Target.execute(Target.java:449)
    at org.apache.tools.ant.Target.performTasks(Target.java:470)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1401)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1374)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1264)
    at org.apache.tools.ant.Main.runBuild(Main.java:818)
    at org.apache.tools.ant.Main.startAnt(Main.java:223)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:284)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:101)

Total time: 2 seconds

Solution

  • I solved the problem, first by checking the build.xml, the section where run is defined :

    105.  <target name="run" depends="init, compile, jar, copy configs">
    106.    <java fork="yes" dir="${build}" classname="org.contikios.cooja.Cooja" maxmemory="512m">
    107.      <sysproperty key="user.language" value="en"/>
    108.      <arg line="${args}"/>
    109.      <env key="LD_LIBRARY_PATH" value="."/>
    110.      <classpath>
    111.        <pathelement path="${build}"/>
    112.        <pathelement location="lib/jdom.jar"/>
    113.        <pathelement location="lib/log4j.jar"/>
    114.        <pathelement location="lib/syntaxpane-1.2.0.jar"/>
    115.        <pathelement location="lib/swingx-all-1.6.4.jar"/>
    116.      </classpath>
    117.    </java>
    118.  </target>
    

    and you can see that its depends on jar.

     18.   Build COOJA (dist/cooja.jar) and all default projects (MSPSim et al)
     19.   > ant jar
    

    so checking the jar section :

    196.  <target name="jar" depends="jar_cooja">
    197.    <ant antfile="build.xml" dir="apps/mrm" target="jar" inheritAll="false"/>
    198.    <ant antfile="build.xml" dir="apps/mspsim" target="jar" inheritAll="false"/>
    199.    <ant antfile="build.xml" dir="apps/avrora" target="jar" inheritAll="false"/>
    200.    <ant antfile="build.xml" dir="apps/serial_socket" target="jar" inheritAll="false"/>
    201.    <ant antfile="build.xml" dir="apps/powertracker" target="jar" inheritAll="false"/>
    202.  </target>
    

    I can see that it try to manipulate the ./apps/serail_socket but it failed. so I checked then the owner and the right permission of those directories:

    $ ls -li | awk '{print $1, $3, $4, $NF}'
    total   
    drwxr-xr-x root root build
    -rw-r--r-- user user build.xml
    -rw-r--r-- root root cooja.config
    drwxr-xr-x root root java
    drwxr-xr-x root root lib
    -rw-r--r-- root root serial-socket.jar
    

    The owner was root and not the normal user. maybe I ran the :
    git submodule update --init --recursive command by mistake with sudo or something. so I changed the owner of those files and directories :
    sudo chown -h -R user ./apps/serail_socket ./apps/powertracker
    And then its worked as it should.