Question

Does anyone know how to make eclipse or netbeans use the graphics card in optimus laptops by invoking optirun (bumblebee) inside the IDE so that one can just use the run button in the IDE to run the program in a graphics card within the IDE.

In simplest form I just want the IDE to do the equivalent of optirun ./javaproject

Was it helpful?

Solution

The way I did this in Eclipse was to first start the Java debugger jdwp and listen to a port. Then start the JVM with optirun java ... and use jdwp to connect to this port. Both tasks can be started at the same time in Eclipse by creating a Launch Group in the debug configuration settings (Run -> Debug Configurations). In detail:

  1. Create a Remote Java Application debug configuration with "Standard (Socket Listen)" Connection Type and some arbitrary port, e.g. 56789. This attaches the Java debugger jdwp on port 56789 to a virtual machine which accepts debug connections at this port.
  2. Now we need to start a JVM with optirun. This can be done with a External Tool Configuration (Run -> External Tools -> External Tool Configurations). Create a new Program configuration in the left side of the External Tools Configurations window. You could directly start optirun java <additional arguments> by filling in the required fields. However, I have decided to use a shell script which is reusable by different projects (As can be seen below, there is one part missing to make it entirely reusable. I'm glad for any help from more experienced Eclipse users...). Hence, the Location field points to this shell script. The script itself accepts three arguments: the classpath for the project, the name of the Java executable, and the port number. These arguments can be passed to the script in the Arguments field of the Main tab, e.g.

    • ${project_classpath:${selected_resource_name}}
    • ExecName
    • 56789

    The shell script looks like this, assuming optirun is in your PATH:

    #!/bin/sh
    CLASS_PATH=${1}
    JAVA_EXECUTABLE=${2}
    PORT=${3}
    # TODO: fix this java library path: pass it as an argument as well. Is there an Eclipse variable which stores this?
    JAVA_LIBRARY_PATH=/usr/local/share/OpenCV/java
    #------------------------------------------------------------------------------
    optirun ${JAVA_BIN} -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:${PORT} -Djava.library.path=${JAVA_LIBRARY_PATH} -Dfile.encoding=UTF-8 -classpath ${CLASS_PATH} ${JAVA_EXECUTABLE}
    #------------------------------------------------------------------------------
    
  3. Finally, the two pieces are brought together in a Launch Group in the Debug Configurations window (Run -> Debug Configurations). Create a new Launch Group and add the two previously generated Debug configurations by clicking on Add in the Launches tab and by selecting the appropriate configurations. Note that due to the classpath variable in step 2 (i.e. ${project_classpath:${selected_resource_name}}), the appropriate package needs to be selected in the Package Explorer before clicking on the run debug configuration button (make sure that the Launch Group is selected).

This solution works perfectly for me: I can debug Java code inside Eclipse which calls native code involving CUDA optimizations and Bumblebee only activates the discrete graphics card when necessary.

OTHER TIPS

Just use optirun to start the IDE. For example, optirun eclipse or optirun netbeans

I build the project in Netbeans (F11) and run the following in a terminal:

optirun java -jar path/to/javaproject/dist/javaproject.jar

Mind that if you have any java parameters in your project, you need to add it manually. My workflow is like this:

Locate the Java options from the project, open Project -> Properties, Run. At VM Options I see -Djava.library.path=lwjgl/native/windows;:lwjgl/native/linux. I also have some parameters that I want to pass to main(String[]). With this information, I open a terminal and run:

cd path/to/javaproject
optirun java -Djava.library.path=lwjgl/native/windows;:lwjgl/native/linux \
    -jar dist/javaproject.jar some paremeters

Another hint, if you have to open and close the program frequently, run optirun bash in a different tab so that preparing the use of the graphics card becomes faster. Alternatively, you can run optirun netbeans, but that means that the nvidia card will always be on even if you are programming which increases power use and increase the heat.

Important: if you are using a 32-bit JVM or Java libraries on a 64-bit machine, you also need to install the 32-bit drivers and libraries. For Ubuntu, the nvidia package already contains 32-bit drivers, see this answer. For other distros, you likely need to install lib32-* packages for Mesa, VirtualGL and nvidia-utils.

You can also rename java to java_real and use this portion of code as your java command :

#!/bin/bash

path=$(dirname $(readlink -f $0))
args=""
runner="$path/java_real"
for var in "$@"
do
    if [ "$var" = "-3d" ]; then
    runner="primusrun $runner"
  else
    args="$args $var"
  fi
done

$runner $args

NOTE : I had to do this in /usr/lib/jvm/java-7-openjdk-amd64/jre/bin, not in /usr/bin to make it work with Eclipse.

In Eclipse, just add "-3d" in your program arguments and you're good to go !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top