This is a collection if various handy commands and shortcuts I’ve discovered while working with Ubuntu, Java, SVN, MVN and other tools and applications at work and at home. They are usually tasks that I don’t do so often that I remember how, but do frequently enough to make it annoying to have to search Google every time. I’ve also discovered a few things that made me go “hey, that was really handy, why didn’t I know that years ago”. So I decided to create this page, partly to share the goodies and partly for my own sanity’s sake. It probably should have been a wiki, and maybe it will some day.
If you know a handy tip or trick for power-users, please leave it in the comments below and I might add it to the collection. Thank you!
Friendly DISCLAIMER: All Ubuntu tips might work fine in other Linux distributions, but they are only tested on Ubuntu. Some of the commands and configuration changes mentioned below can seriously mess up your system. Always back up the files you plan to edit before you actually make any changes to them.
Table of contents
- Ubuntu: Disable a startup of service.
- Maven2: Get Jetty to load log4j.properties.
- Maven2: Debug web application running on Jetty.
- SNV: View changes from a specific revision to the HEAD revision.
- Linux: Check what graphics card is installed in the system.
- Ubuntu 10.04: Move the windows buttons back to the right.
- Ubuntu: Control screen brightness on a Lenovo T510.
- Maven2: Building without running tests.
- Linux: What version of my Linux distribution am I running?
- Windows: How to modify the routing table.
- Linux: How to batch resize images from the command line
- Maven2: Dependency Scopes
- Maven2: How to download the J2EE 5/6 dependencies
- IntelliJ IDEA: Compilation fails with the message “Process terminated with exit code 3″
- Microsoft SQL Server: Removing -2 SPID locks
- Java: SEVERE: WSS1906: Invalid key provided for encryption/decryption.
- Maven: Increase JVM Heap Size and PermGen Space (Windows).
- Java: The Most Common Java Keytool Keystore Commands
- Linux: Check disk space
- Linux: Create soft link
- Windows: How to flush DNS cache
- DNSMasq: Routing DNS names to local IPs
- Glassfish: How to prevent the damn admin console from hanging
- Glassfish: How to enable secure admin
- Glassfish: Admin REST services return 400 Bad Request
- Maven: How to build a specific submodule and its dependencies
- Maven: Continue on test failure
- Eclipse: Wipe all cached data from the command line on startup
- LEDE: Upgrade all packages
- Quickly create favicons for your website
- MySQL: Update AUTO_INCREMENT value
Ubuntu: Disable a startup of service.
$ update-rc.d -f remove
Maven2: Get Jetty to load log4j.properties.
I could not for the life of me get Jetty to load the log4j.properties file – even if it was available in the classpath. The solution was to set the location of the file as a system property when starting Maven.
$ mvn -Dlog4j.configuration=file:./target/classes/log4j.properties jetty:run
Alternatively, you can configure this in your Jetty configuration on the Maven POM file inside the configuration element of the Jetty-plugin:
... log4j.configuration file:./target/classes/log4j.properties ...
This way you will not have to add the -Dlog4j system property when starting Jetty from Maven.
Maven2: Debug web application running on Jetty.
Handy when you have to debug a web application running on Jetty in Maven. First we need to export a few MVN_OPTS.
$ export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n"
Then start Maven and Jetty in the normal way.
$ mvn jetty:run
SNV: View changes from a specific revision to the HEAD revision.
$ svn log -r -v
$ svn log -r 15384 -v
Linux: Check what graphics card is installed in the system.
$ lspci | grep VGA
Ubuntu 10.04: Move the windows buttons back to the right.
This tip is Ubuntu 10.04 specific, but it’s very likely that it will also work on 10.10 and any later releases with the windows buttons moved to the left. Here’s how to move them back to the right.
Find to the folder apps/metacity/general and change the button_layout item value to menu:maximize,minimize,close
Ubuntu: Control screen brightness on a Lenovo T510.
If you have a Lenovo T510 with a Quadro NVS 3100M graphics card, like me, you might have some serious issues with controlling the screen brightness – also just like me. Using the Fn key plus the brightness controls doesn’t do a thing, expect for showing you a screen indication that the brightness should change. To fix it, simply add the following line to the “Device” section in /etc/X11/xorg.conf:
Option "RegistryDwords" "EnableBrightnessControl=1"
Save and restart X (or simply restart your laptop if you don’t know how to restart X). It’s quite possible that you have to do this every time the NVIDIA drivers are updated, but that’s OK. We still love them for providing top notch drivers for Linux.
Maven2: Building without running tests.
Of course you shouldn’t skip running the tests when you build a project, but sometimes you have no choice. Like when someone on your team checked in a code change that breaks the tests and left for a dentist appointment and you can’t figure out what the hell is going on. To skip the tests, add -Dmaven.test.skip=true, like this:
$ mvn -Dmaven.test.skip=true clean install
Linux: What version of my Linux distribution am I running?
$ lsb_release -a
Windows: How to modify the routing table.
At work we’re currently buried knee deep in testing. During some of the manual tests, we want to simulate network connection problems to see how the application will behave. In a perfect world, this would be a matter of unplugging the network cable, but we’re using terminals connected to virtual Windows servers. So, how to simulate network problems in such an environment? Easy as pie! First, find the IP of the server the application will normally try to connect to. In this case, we use 192.168.123.50. Then you manually corrupt Windows’ routing table with the following command.
route add 192.168.123.50 MASK 255.255.255.255 192.168.123.2
Make sure the last IP address is an IP that is on the same subnet as the first IP and that it’s not in use, or at least a server that doesn’t have the same service running as the server your application would normally connect to – or else you will start to rip your hair out. All requests to 192.168.123.50 will now be routed to 192.168.123.2 and you have yourself a good old simulated network problem
To clear up the mess, simply remove the entry from the routing table with the following command.
route delete 192.168.123.50
Linux: How to batch resize images from the command line
For this neat trick, we’ll use ImageMagick. It’s mostly likely already installed on your system, if not you can install it by running this command:
$ sudo apt-get install imagemagick
When that is done, navigate to the directory containing the images you want to resize. The command below will overwrite the original images, so make sure you have backups before you start! Run the following command to resize all images with an JPG extension to 25% of their original size:
$ mogrify -resize 25% *.JPG
It should also be possible to substitute 25% with absolute pixel values if that is necessary, like this:
$ mogrify -resize 640x480 *.JPG
Maven2: Dependency Scopes
I can’t seem to ever remember all the Maven dependency scopes and what they mean. So here they are:
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
- import (only available in Maven 2.0.9 or later)
This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
Maven2: How to download the J2EE 5/6 dependencies
If you use Maven2 to create Java Enterprise applications and don’t want to sell your sole to a specific application server, you can download the J2EE 5 and 6 JARs from the download.java.net repository. First, add the java.net Maven2 repository to your POM:
Then, add the dependency you need to your POM.
javaee javaee-api 5
javax javaee-api 6.0
IntelliJ IDEA: Compilation fails with the message “Process terminated with exit code 3”
The solution to this problem is to give IntelliJ more heap memory for compiling. You can increase this value in Settings | Compiler (source)
Microsoft SQL Server: Removing -2 SPID locks
If you end up with threads in SQL Server that is blocked by another thread with SPID -2 you either have to restart the database server or kill the offending SPIDs with some SQL magic. The latter approach is preferred since it won’t terminate all connection to the database. Note that you need access to master..syslockinfo to do this. First, run the follow query:
SELECT req_transactionUOW FROM master..syslockinfo WHERE req_spid = -2 AND req_transactionUOW > '00000000-0000-0000-0000-000000000000'
This will give you the req_transactionUOW of every -2 SPID. Next, kill the transaction lock (I’m not sure what you are actually killing) with the following command:
KILL 'F600C62D-3FC4-4210-9EF2-74FE9DBEF5D6' --Replace the ID with one in your list.
This should release any locks in your database held by a -2 SPID.
Java: SEVERE: WSS1906: Invalid key provided for encryption/decryption.
I got this error when working on connecting a Java client to a MCF webservice that required the use of a secure connection, but you might get a similar error when working on other cryptography-related tasks. To solve this you have to install the strong encryption JARs from Oracle. Download from the Java download page. I’ve not included a direct link to the download itself because Oracle tend to change the URLs.
Maven: Increase JVM Heap Size and PermGen Space (Windows).
Open a command line window and type the following:
set MAVEN_OPTS=%MAVEN_OPTS% -Xmx1024m -XX:MaxPermSize=512m
This will set the JVM heap size to 1024 megabytes. Modify the number based on your needs. The %MAVEN_OPTS%-part makes sure that other MAVEN_OPTS that you have defined are not removed.
Java: The Most Common Java Keytool Keystore Commands
Note: If you prefer to use a graphical user interface to edit the keystore, I recommend KeyStore Explorer.
- Generate a Java keystore and key pairkeytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048
- Generate a certificate signing request (CSR) for an existing Java keystorekeytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr
- Import a root or intermediate CA certificate to an existing Java keystorekeytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks
- Import a signed primary certificate to an existing Java keystorekeytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks
- Generate a keystore and self-signed certificate (see How to Create a Self Signed Certificate using Java Keytool for more info)keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
- Check a stand-alone certificatekeytool -printcert -v -file mydomain.crt
- Check which certificates are in a Java keystorekeytool -list -v -keystore keystore.jks
- Check a particular keystore entry using an aliaskeytool -list -v -keystore keystore.jks -alias mydomain
- Delete a certificate from a Java Keytool keystorekeytool -delete -alias mydomain -keystore keystore.jks
- Change a Java keystore passwordkeytool -storepasswd -new new_storepass -keystore keystore.jks
- Export a certificate from a keystorekeytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks
- List Trusted CA Certskeytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
- Import New CA into Trusted Certskeytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts
Linux: Check disk space
$ df -h
Linux: Create soft link
$ ln -s
Windows: How to flush DNS cache
C:\> ipconfig /flushdns
DNSMasq: Routing DNS names to local IPs
This can be very convenient if you, like me, host a server on your local LAN and have Apache configured with virtual hosts on different domains. I have my router with DD-WRT and DNSMasq configure in such a way that if I do lookups for any of my domains while I’m on my local network, I get routed to the local IP and not the public IP, which would just screw tings up.
You can add as many domains as you wish (source).
Glassfish: How to prevent the damn admin console from hanging
This is caused by the admin console looking for updates when you log in. If your server is not connected to the internet, you’re behind a proxy that has not been configured or Oracle’s update servers are down, login in can take ages or never happen at all. Here’s how to get rid of this huge annoyance:
- In $GLASSFISH_HOME/glassfish/modules, rename the file console-updatecenter-plugin.jar to console-updatecenter-plugin.jar.old.
- Restart Glassfish.
You should now be able to log in quickly no matter the state of your internet connection and Oracle’s servers. Source.
Glassfish: How to enable secure admin
asadmin --host localhost --port 4848 enable-secure-admin
Glassfish: Admin REST services return 400 Bad Request
So, you had everything working in Glassfish 3.0, but after an upgrade to 3.1 all of your requests to the admin REST services return 400 Bad Request? Try to add the following header to your request:
X-Requested-By:GlassFish REST HTML interface
Maven: How to build a specific submodule and its dependencies
Example: You have a huge Maven project with plenty of modules that uses intricate internal dependencies but you only want to build parts of it (say, the web services, but not the thick client). Building the correct modules can be a pain and building the entire project to make sure all dependencies are built can take forever. Thankfully, the Maven reactor can analyze your POM files and automagically build only the necessary modules in your project:
$ mvn -pl -am
$ mvn clean install -pl submodule/another-module,submodule2/the-second-module -am
Maven: Continue on test failure
$ mvn -Dmaven.test.failure.ignore
Eclipse: Wipe all cached data from the command line on startup
Using Eclipse is a painful experience, but every now and then you’re forced into using it. Occasionally and without any warning, Eclipse tends to go full “I can’t let you do that, Dave” and refuses not work properly until you wipe its memory. Close Eclipse and run it again from the command line with the -clean flag (source) to get the application to behave again.
$ eclipse -clean
LEDE: Upgrade all packages
Log in to the router as root via SSH, and run the following two commands (source).
$ opkg update $ opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade
There’s a good chance the router has to be rebooted for some of the updated packages to be effective.
Quickly create favicons for your website
If you need to create favicons, a service like iconifier.net can be quite handy. Upload a picture, and it’ll generate a bunch of different favicons, and provide you with instructions on how to use them.
MySQL: Upgrade AUTO_INCREMENT value
ALTER TABLE picture AUTO_INCREMENT=1972