Using Bumblee (Graphics Drivers) with Eclipse IDE [duplicate] - java

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

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:
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.
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}
#------------------------------------------------------------------------------
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.

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 !

Related

how to start a java application system-independent at Windows startup

I want to automatically run a Java application under Windows at startup time, but not depending on a specical Windows or a special JRE-version.
Generally, a good way to run programs at Windows login time would be to add a registry entry under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But it seems as if the system PATH is not evaluated when Windows tries to run the programs listed in the registry's RUN section.
So these both entries do work:
"C:\Program Files\Java\jdk1.8.0_111\bin\javaw" -jar c:\path\to\java.jar
"C:\ProgramData\Oracle\java\javapath\javaw" -jar c:\path\to\java.jar
But this one doesn't come up:
javaw -jar c:\path\to\java.jar
Now, the latter works when manually entered in a console window because there the required PATH exists and is evaluated:
PATH=....;C:\ProgramData\Oracle\Java\javapath;....
Now, I want to have the registry entry added from an automated installer programm and I would like to have it formed "windows-system-independent" (from Vista up to Win 10, and furthermore not depending from the version of the installed JRE). So I do not know a general path to the JRE and therefore I cannot add a specific RUN-command to the registry.
Does anyone know how to add JRE-calls system-independent correctly to the registry's RUN-section?
I've already tried with a command-file, e.g. "startup.cmd" with content java -jar file.jar there, have added a call of this file to the registry, and yes that worked, java was found, the application was started, but when using this method I have an annoying black command terminal window staying open all the time while the java app is running.
Thanx,
Tombo
Change your startup.cmd, from
java -jar file.jar
to
start java -jar file.jar
This will launch the java.exe in a new process an let your batch executor terminate.

Remote Debugging GUI Java Application (swing) in NetBeans 8

I setup the remote debugging in NetBeans IDE between 2 Linux systems. Remote debugging an application that does not have a GUI works ok, but I am getting this error when I try to remote debug an application that has swing GUI:
Exception in thread "AWT-EventQueue-0"
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
I would appreciate any suggestion!
NetBeans output window:
ant -f /home/toma/NetBeansProjects/GUIFormExamples -Dremote.platform.password=***** -Dremote.platform.rp.target=linux-15 -Dremote.platform.java.spec.ver=17 -Dremote.platform.rp.filename=linux -Ddebug.class=Antenna -Dnb.internal.action.name=debug debug-remote
init:
Deleting: /home/toma/NetBeansProjects/GUIFormExamples/build/built-jar.properties
deps-jar:
Updating property file: /home/toma/NetBeansProjects/GUIFormExamples/build/built-jar.properties
compile:
Copying 1 file to /home/toma/NetBeansProjects/GUIFormExamples/build
Copy libraries to /home/toma/NetBeansProjects/GUIFormExamples/dist/lib.
To run this application from the command line without Ant, try:
java -jar "/home/toma/NetBeansProjects/GUIFormExamples/dist/GUIFormExamples.jar"
jar:
Connecting to 192.168.1.122:22
Connecting to 192.168.1.122:22
cmd : mkdir -p '/home/toma/NetBeansProjects//GUIFormExamples/dist'
Connecting to 192.168.1.122:22
done.
profile-rp-calibrate-passwd:
Connecting to 192.168.1.122:22
cmd : cd '/home/toma/NetBeansProjects//GUIFormExamples'; '/usr/lib/jvm/j2sdk1.7-oracle/jre/bin/java' -Xdebug -Xrunjdwp:transport=dt_socket,address=localhost:39245 -Dfile.encoding=UTF-8 -jar /home/toma/NetBeansProjects//GUIFormExamples/dist/GUIFormExamples.jar
Exception in thread "AWT-EventQueue-0"
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
at java.awt.Window.(Window.java:535)
You need to add DISPLAY environment variable export to your ant build xml file. I have blogged about this issue and it's solution on my blog
For my project there was a target named "-copy-to-remote-platform" and two macros in it: "runwithpasswd" and "runwithkey" in ANT build xml file which required some modifications.
I have added "export DISPLAY=:0;" to last sshexec commands in each of the aforementioned macros so that they both looked like this:
<sshexec host="${remote.platform.host}" port="${remote.platform.port}" username="${remote.platform.user}" password="${remote.platform.password}" trust="true" usepty="true"
command="export DISPLAY=:0; cd '${remote.project.dir}'; ${remote.platform.exec.prefix}'${remote.java.executable}' #{additionaljvmargs} -Dfile.encoding=${runtime.encoding} ${run.jvmargs} ${run.jvmargs.ide} -jar ${remote.dist.jar} ${application.args}"/>
Mind the "export DISPLAY=:0;" on the beginning of the "command" attribute.
You need to use X11 forwarding, to make the GUI on the remote computer visible on your computer (or otherwise give a valid DISPLAY environment variable, so the GUI can be displayed somewhere). This is a Linux configuration issue, Java is just complaining that it can't create a GUI, because (as far as it knows) there is no screen available.
Success! It is not as easy as it should be, but it works.
In NetBeans (I used version 8) create a new Java Platform for remote debugging: Tools -> Java Platforms -> Add Platform -> Remote Java Standard Edition -> ... (for more info see this link: https://netbeans.org/kb/docs/java/javase-embedded.html). Press the drop-down menu on the Debug Icon (debug-remote) and watch the output window.
That works well if the program does not have a GUI.
If the program has a GUI, I get this error: "java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it"
Even if the program has a GUI, the steps above help, because it automatically deploys your program on the remote server.
To debug the GUI, the workaround I found, is to remotely connect to the server using ssh or VNC and run the java program in debug mode:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y -jar "/MyPath/Program.jar".
Java VM should pause the program and wait for NetBeans debugger to connect.
In NetBeans, set a break point in the program and attach to the remote process: Debug -> Attach Debugger -> Java Debugger, SocketAttach, dt_socket, IP address of the server, Port: 4000 -> OK
Later on you can just press the drop-down menu on the Debug Icon (Attach to ...) to start debugging.
The program should run to the break point and pause execution. You should have full control over the GUI on the programming computer but the program is executed on the server.
This is very useful when debugging Java programs on single board computers like BeagleBone Black or Raspberry PI that do not have enough horsepower to run NetBeans. This is essential when the single board computer is used in a robotics application and it needs to receive sensor inputs and control motors.
The Solution:
Go to Run → Set Project Configuration → Customize..
Click Manage Platforms
Select the remote configuration for your RPI
On right sight go to Exec Prefix and write startx in it.
Problem is that NetBeans single-quotes everything that you put into the Exec Prefix field. So you can put in your own single-quotes to construct a valid bash command. Imagine you have a VNC virtual desktop at display :2.0, you can trick it out with
export' DISPLAY=:2.0;'sudo
Finally this will result in a working bash command string with a quoted export and a quoted sudo (which does no harm). If you don't want your program to run in superuser mode, change it to
export' DISPLAY=:2.0;sudo -u 'pi
I found only the sudo command as a working way to cope with these single quotes.

Application runs differently out of Netbeans

I need to run my application with those 2 VM options:
(-XX:+UseConcMarkSweepGC -Xmx2048m).
I set them into "project properties -> run -> VM options" in Netbeans.
Then when i run it in Netbeans it works great but out of Netbeans or on another pc it doesn't.
Is it possible to make my application run always with those VM options?
(without setting it in console before running application)
How does it need to be done? (My project properties method seems wrong)
Probably the easiest way is to create a simple batch file, that executes the Java application with relevant options.
java -XX:+UseConcMarkSweepGC -Xmx2048m -jar MyApp.jar
Save this as MyApp.cmd (assuming Windows operating system here) in the same folder as the JAR file and that should be it - just double-click the .cmd file.
This method also needs to have Java in the OS path, but this should mostly be the case nowadays with default java installations.

Eclipse Command Line Java

I would like to see the command that Eclipse is running when I hit run for a Java program. I've looked around the Eclipse preferences for Run/Debug (and console) and the Run Configurations, but to no avail. How can I see the line that Eclipse is using to launch?
In JBuilder, it was the first line in the output.
Edit: I'm not asking whether it uses javac to compile and then java to run. I want to see the line that starts with java and has all the flags, etc. I'm not asking "what does Eclipse run?" since I already know that. I want to see it in a specific case in a specific project.
Set up a launch configuration, then run or debug it.
Go to the "Debug" window of the Debug perspective, the one that shows all the processes and threads.
Right click on the java.exe or javaw.exe item in the tree (its at the bottom under all of the threadgroups and threads), and choose "Properties" on that guy.
You should get a window that contains 2 sections, the left being a list of items, including "process information" and "vm capabilities"
The process information section has 3 sections, showing the time it launched the session, the path to the exe, and the full command line that eclipse used to start the VM. The command line will include everything, including library paths, classpaths, the debug info it passes to the VM, any custom args you pass, etc.
I think that's what you're looking for.
On Unix systems you can see the command line with
ps -e x | grep java
For example (line wrapped for readability):
24925 pts/6 Sl 0:16
/usr/lib/jvm/java-6-openjdk/bin/java
-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:53880
-Dfile.encoding=UTF-8
-Xbootclasspath:/usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar
:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar
:/usr/lib/jvm/java-6-openjdk/jre/lib/jsse.jar
:/usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar
:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar
:/usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar
:/usr/share/java/gnome-java-bridge.jar
-classpath /home/hendrik/workspace/webflag/WEB-INF/classes
:/home/hendrik/workspace/webflag/WEB-INF/lib/log4j.jar
:/home/hendrik/workspace/webflag/WEB-INF/lib/junit.jar
nhb.webflag.importtools.tools.ImportArmoryCharacter
-agentlib specifies the debugging connection, -Xbootclasspath is based on the JDK configuration, -classpath based on the build path settings of the project
If it can find any class with the main method, it runs that using "java com.example.Main" where Main is the class with main method.
If you have many classes with main method, then eclipse gives you an option to choose one.

Run GWT shell normal, use -noserver on compile?

I would like to run the GWT shell (the nice little Google pop-up browser window) when "testing" and upon clicking the actual "Compile/Browse" on the tool, compile the program into a different directory and use the -noserver mode to test it on my IIS server.
Problem:
I have not edited the normal shell script, so it fires off just fine. The edits I make are viewed upon refreshing. The compile/browse however, shoots me off to mini-Tomcat server instead of my directory. The compile DOES appear to be running (as I can browse to that directory directly and it is receiving updates), but the compile/browse does not shunt me there.
Here is the compile script I've got right now:
#java -Xmx256M -noserver -port 80 -cp "%~dp0\src;%~dp0\bin;C:/dev/gwt-windows-1.5.3/gwt-user.jar;C:/dev/gwt-windows-1.5.3/gwt-dev-windows.jar" com.google.gwt.dev.GWTCompiler -out c:/dev/Practice /Practice/Practice.html
Clearly the area I would LIKE to go is "http://localhost/Practice/Practice.html" (the IIS server) but it is jumping to "http://localhost:8888/practice.Practice/Practice.html" (the Tomcat server).
Ideas?
(P.S. Running Eclipse for editor, the Run/Debug config arguments are:
practice.Practice/Practice.html
and that's it)
You can make an Eclipse run profile for the GWTCompiler class. One of the arguments you pass in is the path to compile to, so you could run the compile from Eclipse and it would write the files directly to the IIS's folder if you specified it. Once the compile is done, you can use the already open Hosted Mode instance and just type in the http://localhost/Practice/Practice.html path, and it'll work.
The Hosted Mode browser can be pointed at any server even without running it with -noserver, and if you get security warnings about leaving the domain, you can add a -whiteList argument to the Hosted Mode run profile. The security warnings will give you an example whiteList argument.
Try running GWTCompiler and GWTShell with no arguments for the possibilities. I'd be more detailed, but I don't have a dev environment with me right now.

Categories

Resources