RSS Entries RSS
RSS Subscribe by Email

Archive for Tips and Tricks

Trouble Resuming NX Session

Resume button disabled

I was unable to resume my NX session.  While trying to connect, the resume button was greyed out (or grayed out)  This apparently can happen if the two machines are running at a different color depth.

The machine I was trying to connect to was running Ubuntu, so I checked the color depth (sudo cat /etc/X11/xorg.conf) and saw that it was running at 24 bit.  Then I checked the color depth on my Windows machine (Control Panel > Display > Settings > Color Quality) and saw it was running at 16 bit.  There was no option to set it to 24-bit, so I chose the option “Highest (32 bit)”.  No more disabled resume button!

I think the cause of the problem was plugging my laptop into the projector at work yesterday.  It apparently changed my color depth and Windows didn’t reset it when I exited presentation mode.

Execution of last command failed

Trying to reconnect to my NX session kept failing.  I hit the Details button and saw the following:

NX> 596 NX> 596 ERROR: NXNODE Ver. 3.4.0-6  (Error id e76DC5C)
NX> 596 NX> 596 ERROR: resume session: run commands
NX> 596 NX> 596 ERROR: execution of last command failed
NX> 596 NX> 596 last command: '/bin/kill -USR2 16905'
NX> 596 NX> 596 exit value: 1
NX> 596 NX> 596 stdout:
NX> 596 NX> 596 stderr: /bin/kill: 16905: No such process

The only thing I found that would let me connect to my box via NX at all was to “rm /usr/NX/var/db/running/*” in order to kill my current session.  Killing the session is a sucky option, but at least now I can use NX again.

Comments (2)

Printing a Stack Trace anywhere in Java

You don’t need to catch an Exception in order to print a stack trace in Java.  Sometimes they can be helpful for debugging and logging purposes.  Here’s an example of how to print a stack trace at any moment:

new Exception().printStackTrace();

If you want more control over the output, you can build some code off the following:

  System.out.println("Printing stack trace:");
  StackTraceElement[] elements = Thread.currentThread().getStackTrace();
  for (int i = 1; i < elements.length; i++) {
    StackTraceElement s = elements[i];
    System.out.println("\tat " + s.getClassName() + "." + s.getMethodName()
        + "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
  }

Comments (3)

Determining Port Usage

Want to know how to figure out what’s running on a given port on your machine?  The following example will show you what’s running on port 80 on your Linux machine:

lsof -i -n -P | grep :80

Comments (2)

Helpful Bash Aliases

Just a few to post for right now, but this entry may grow later.

alias ll='ls -la'           # list all directory contents in listing format
alias untargz="tar zxvf"    # unpacks a .tar.gz file
alias rd='cd `pwd -P`'      # change to the real directory if in a linked directory

Comments

Essential Linux Commands

Getting started on Linux can be challenging.  Largely because the first time user won’t have any idea how to track down potential problems.  The following commands are essential to get additional information about your system when something goes wrong:

  • uname -mr – Shows what kernel version and processor you are running on
  • df and fdisk -l – Gives you file system info.  Can help you figure out how things are mounted
  • dmesg – Useful for tracking down problems during boot
  • tail -f /var/log/messages – Now run the process giving you problems and you might see helpful error messages
  • top – Shows the programs which are the top memory and CPU users
  • pgrep - Returns the process ids of a given program, allowing you to kill frozen programs

If you’ve got other suggestions, please feel free to comment below.  Thanks!

Comments

Open Windows Explorer to New Default Directory

To get Windows Explorer to open to a default directory, you can create a new shortcut and modify the path. For example, the following shortcut path will open a directory of documents on a shared drive using Explorer instead of the normal window:

%SystemRoot%\explorer.exe /e,S:\Documents

Comments

Change the NetBeans Default JDK

A client sent me some code today to update. He was using the NetBeans, so I downloaded the IDE and fired it up to open the project he’d sent me. Unfortunately, the project wouldn’t compile because he’d written the code in Java 6 while NetBeans was using Java 5. I couldn’t find a NetBeans menu to update the setting, but rather found that the fix is to add the following in NetBean’s etc/netbeans.conf file:

# Default location of JDK, can be overridden by using –jdkhome <dir>:
netbeans_jdkhome=”C:\Program Files\Java\jdk1.6.0_05″

Comments (14)

Intro to URL Rewriting with Apache’s .htaccess

I have created an .htaccess file to do URL rewriting for every site I’ve ever created. If you’re not familiar with URL rewriting, it is used to modify a URL or redirect the user before the requested resource is fetched. One of its major uses is to make URLs human readable. That means your users can visit a pretty URL like http://www.tabworldonline.com/guitar/A/ and have it interpreted by the server as http://www.tabworldonline.com/artists.php?instrument=guitar&letter=A.

Most of the time, this file can be relatively simple. I would always recommend using one for URL canonicalization, which is a fancy term for making sure you have one unique URL for each page. For example, lumidant.com redirects to www.lumidant.com. This is beneficial for SEO because you want to ensure that search engines don’t split your ranking points between pages that are actually one and the same.

The code below is the .htaccess file from this site. The declarations in the file are regular expressions, which you might need to get a quick refresher on if you’re not familiar with. A few other things to be aware of include the fact that [NC] stands for no case and means that the text is not case-sensitive, [R=301] tells the server to do a 301 redirect, and [L] tells the server it can quit there and and not bother processing the rest of the file.

<IfModule mod_rewrite.c>

  RewriteEngine on

  # rewrite all lumidant.com requests to the lumidant subdirectory
  RewriteCond %{HTTP_HOST} ^(www\.)?lumidant\.com$
  # this is needed to stop infinite looping
  RewriteCond %{REQUEST_URI} !^/lumidant/.*$
  # don't redirect these directories to the lumidant subdirectory
  RewriteCond %{REQUEST_URI} !^/pinknews/.*$
  RewriteRule ^(.*)$ /lumidant/$1

  # if you're asking for a directory and there is no trailing slash then add one
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteCond %{REQUEST_URI} !^.*/$
  RewriteRule ^/lumidant/(.*)$ http://www\.lumidant\.com%{REQUEST_URI}/ [R=301,L]

  # add a www if there's not one
  RewriteCond %{HTTP_HOST} ^lumidant\.com$ [NC]
  RewriteCond %{REQUEST_URI} !^/blog.*$
  RewriteRule ^lumidant/(.*)$ http://www\.lumidant\.com/$1 [R=301,L]

</IfModule>

This blog is currently hosted with BlueHost. For accounts with multiple domains, BlueHost places the add-on domains in subdirectories of the main domain. This can be confusing to maintain, so I moved all of the lumidant code to a subdirectory as well and then updated the .htaccess file to make this organization transparent to the end user.

The last few lines add a www to all non-www pages. While I could have placed this at the beginning of the file, the file would be executed again after the redirect causing possibly another redirect to be executed if a trailing slash needed to be added. Keep in mind while organizing the file that you’d like to minimize the number of redirects for many reasons including response times, reducing server load, and optimizing for search engines.

URL rewriting can be tricky at first, especially if you’re not familiar with regular expressions. If you’re working with redirections, then it may help to check the HTTP headers of your request to see what intermediate redirects are occurring.

Finally, if you’re not using Apache there are other alternatives to .htaccess. For example, I have used the UrlRewriteFilter in the past for Java web apps.

Comments

Suppressing Compile Warnings with Java Annotations

If you’ve used Java 1.5 Generics much then you’re probably familiar with the following compile warning: “Type safety: The expression of type List needs unchecked conversion to conform to List<String>” or similar. It turns out there’s a rather simple solution with annotations to ignore this problem:

@SuppressWarnings(“unchecked”)

A couple other possible uses of the annotation that might be of interest are:

@SuppressWarnings(“deprecation”)
@SuppressWarnings(“serial”)

These are compiler specific, so you may want to check out the full Eclipse list, which is a bit lengthier than Sun’s 7 options (all, deprecation, unchecked, fallthrough, path, serial, and finally).

Also, multiple statements can be combined into one as follows:

@SuppressWarnings({“unchecked”, “deprecation”})

Comments (1)

Adding Links for Navigation to the WordPress Pages List

The category titled “Pages” on the right-hand side of this blog is an area where WordPress will allow a user to create wiki pages. However, I wanted to include some navigational links in that area as well, which isn’t immediately available through the WordPress admin screens.

The solution I found was to create a new blogroll category and use it for navigational links. First off, I modified the call to wp_list_pages by adding the argument “title_li=0″, which tells WordPress not to wrap it in <ul> tags, but to instead only output the <li> tags. Then I wrapped the call with my own <ul> tags. Finally, I called wp_list_bookmarks in order to display the category I had created, which had an id of 34. I again used the “title_li=0″ parameter and also had to add “categorize=0″, so that this parameter would not be ignored:

	<li id="pages">
		<h2><?php _e('Pages'); ?></h2>
		<ul>
			<?php wp_list_bookmarks('categorize=0&title_li=0&category=34'); ?>
			<?php wp_list_pages('title_li=0'); ?>
		</ul>
	</li>

Comments