Tips & Tricks for Power-users.

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

  1. Ubuntu: Disable a startup of service.
  2. Maven2: Get Jetty to load log4j.properties.
  3. Maven2: Debug web application running on Jetty.
  4. SNV: View changes from a specific revision to the HEAD revision.
  5. Linux: Check what graphics card is installed in the system.
  6. Ubuntu 10.04: Move the windows buttons back to the right.
  7. Ubuntu: Control screen brightness on a Lenovo T510.
  8. Maven2: Building without running tests.
  9. Linux: What version of my Linux distribution am I running?
  10. Windows: How to modify the routing table.
  11. Linux: How to batch resize images from the command line
  12. Maven2: Dependency Scopes
  13. Maven2: How to download the J2EE 5/6 dependencies
  14. IntelliJ IDEA: Compilation fails with the message “Process terminated with exit code 3″
  15. Microsoft SQL Server: Removing -2 SPID locks
  16. Java: SEVERE: WSS1906: Invalid key provided for encryption/decryption.
  17. Maven: Increase JVM Heap Size and PermGen Space (Windows).
  18. Java: The Most Common Java Keytool Keystore Commands
  19. Linux: Check disk space
  20. Linux: Create soft link
  21. Windows: How to flush DNS cache
  22. DNSMasq: Routing DNS names to local IPs
  23. Glassfish: How to prevent the damn admin console from hanging
  24. Glassfish: How to enable secure admin
  25. Glassfish: Admin REST services return 400 Bad Request
  26. Maven: How to build a specific submodule and its dependencies
  27. Maven: Continue on test failure
  28. Eclipse: Wipe all cached data from the command line on startup
  29. LEDE: Upgrade all packages

Ubuntu: Disable a startup of service.

$ update-rc.d -f <service name in /etc/init.d> 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:

<configuration>
    ...
    <systemProperty>
        <name>log4j.configuration</name>
        <value>file:./target/classes/log4j.properties</value>
    </systemProperty>
    ...
</configuration>

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 <revision number> -v

For example

$ 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.

$ gconf-editor

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:

  • compile
    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.
  • provided
    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.
  • runtime
    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.
  • test
    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.
  • system
    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:

<repository>
    <id>download.java.net</id>
    <url>http://download.java.net/maven/2/</url>
</repository>

Then, add the dependency you need to your POM.

J2EE 5:

<dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
</dependency>

J2EE 6:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
</dependency>

(source)

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 pair

    keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048

  • Generate a certificate signing request (CSR) for an existing Java keystore

    keytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr

  • Import a root or intermediate CA certificate to an existing Java keystore

    keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks

  • Import a signed primary certificate to an existing Java keystore

    keytool -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 certificate

    keytool -printcert -v -file mydomain.crt

  • Check which certificates are in a Java keystore

    keytool -list -v -keystore keystore.jks

  • Check a particular keystore entry using an alias

    keytool -list -v -keystore keystore.jks -alias mydomain

  • Delete a certificate from a Java Keytool keystore

    keytool -delete -alias mydomain -keystore keystore.jks

  • Change a Java keystore password

    keytool -storepasswd -new new_storepass -keystore keystore.jks

  • Export a certificate from a keystore

    keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks

  • List Trusted CA Certs

    keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts

  • Import New CA into Trusted Certs

    keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts

(Source.)

Linux: Check disk space

$ df -h

Linux: Create soft link

$ ln -s <target filname> <symbolic filname>

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.

address=/vegard.net/buingo.com/vbox-host.com/192.168.1.3

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:

  1. In $GLASSFISH_HOME/glassfish/modules, rename the file console-updatecenter-plugin.jar to console-updatecenter-plugin.jar.old.
  2. 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

Source

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 <maven goals> -pl <list of relative module names> -am

Example:

$ mvn clean install -pl submodule/another-module,submodule2/the-second-module -am

Source.

Maven: Continue on test failure

$ mvn <maven goals> -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.

Write a Comment

Comment

+ 45 = 48