Jason’s Ranting & Raving

Those who don’t read have no advantage over those who can’t.

Posts Tagged ‘software’

Programming: Android setup for Ubuntu

Posted by jaystile on September 27, 2010

Android Setup
I’ve been kicking the idea around of developing some Android applications. This blog post is about setting up that initial environment.

After a quick google search, I found the Android developer page. Downloaded the Linux (i386) SDK. I was thinking that I might want to develop on Windows, but I already had a VirtualBox running Ubuntu 10.04 with the Required Eclipse and JDK. I browsed the System Retirements page http://developer.android.com/sdk/requirements.html, and it looks the VM (Virtual Machine) will be just fine.

Java development downloads go to into the /opt directory on this linux VM. Softlinks are used to help maintain version of the software.

/opt/ant/latest → /opt/app/apache-ant-1.8.1
/opt/java/latest → /opt/java/jdk1.6.0_21
/opt/eclipse/latest →/opt/eclipse/eclipse-jee-helios

The ~/.bashrc file gets updated to add variables to these locations and then export the $PATH so you can execute them on the command line.

export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH

Download the Android SDK

$ mkdir /opt/android/
$ wget http://dl.google.com/android/android-sdk_r07-linux_x86.tgz
$ tar zxvf android-sdk_r07-linux_x86.tgz
$ mv android-sdk-linux_x86 android-sdk-linux_x86-0.9.8 
$ ln -s android-sdk-linux_x86-0.9.8/ latest

ADT Plugin v 0.9.8
The Eclipse workspace has been setup to use /opt/workspace/android. Eclipse then needed to include the ADT plug-in . Add the site: https://dl-ssl.google.com/android/eclipse/ to Eclipse after selection Help → Install Software.

Install SDK Tools r7 into your SDK
The link describes adding new tools into your sdk http://developer.android.com/sdk/adding-components.html#UpdatingComponents .

Launch the android manager.

$ /opt/android/latest/tools/android

It looks like the Android SDK Tool revision 7 was already there! Moving on.
I got a pop-up from the Plug-in asking to enable statistics usage. But it was ‘unclickable’. So it’s just hanging out there now, so I’ll have to kill it from the command line. I could tell if it came from the Android manager or the ADT eclipse plugin.

First Project
Used Eclipse to try and create a new Android Project. This failed because an SDK was not defined.
Register SDK with Eclipse
Window → Preferences → Android
SDK Location: /opt/android/latest
Failed to get the adb version: Cannot run program “/opt/android/latest/tools/adb”: java.io.IOException:error=2, No such file or directory.
That is very odd. The file is navigable from the command line. However, when trying to execute it from the command line like ./adb, the bash: ./adb: No such file or directory error occurs.
Attempting to restart (and kill that pop-up that is still hanging around).

$ /opt/android/latest/tools/android

Install packages
Installed Packages → Android SDK Tools, revision 7. Clicked update all and selected all recommended downloads.
Then removed all obsolete packages.

Try Again – With Galileo
Downloaded Eclipse JEE Galileo, I heard rumor through the grapevine that ADT Plugin did not play nice with Helios.
This still did not resolve the original issue of the adb not working.

Add the site: https://dl-ssl.google.com/android/eclipse/ to Eclipse after selection Help → Install Software. In this instance, I made it to the license agreement. However, when selecting the radio, “I agree” and scrolling down to the bottom of the license window, the ‘Finish’ button was never enabled. Thank you Mylyn for having 9 things to agree to when all I’m trying to is NOT add your plug in. It is my professional opinion that Eclipse with JEE comes with way too many plug-ins that do not have value add. Eclipse is pretty stable, but the plug-in developers really need to get on the ball. I keep running into resource hogs or plug-ins that don’t do their job very well. Have you seen the GIT plug-in? It doesn’t even come close to mapping what you can do on the command line. Going to get Eclipse just for Java to minimize plug-ins to see if I can get this to install.

Try Again – With Galileo sans JEE garbage.
Installed the basic Eclipse IDE for Java Developers. There are noticably less plug-ins install that might collide. But Mylyn is still there. Let’s see how it goes. Oh look at that! It appears that I am in the same situation. Here are the list of things to agree to:

Android DDMS
Android Development Tools
Mylyn Bridge: Eclipse IDE
Mylyn Bridge: Java Development
Mylyn Bridge: Team Support
Mylyn Connector: Bugzilla
Mylyn Task List (Required)
Mylyn Task-Focused Interface (Recommended)
Mylyn WikiText

Let’s see if my outcome is any different this time. I’ll scroll to the bottom of each agree and check I accept. Wow! Different behavior! The ‘Finish’ button was able to be clicked. Joyous happy day.

Back on track – Setup the SDK
Window → Preferences → Android
SDK Location: /opt/android/latest

Configure the SDK
Eclipse → Window → Android SDK and AVD Manager
Something weird happened again.
After selecting the option above I received and error. [2010-09-17 06:12:09 – adb]Failed to get the adb version: Cannot run program “/opt/android/latest/tools/adb”: java.io.IOException: error=2, No such file or directory
After selecting it a second time, the application launched.
Android SDK and AVD Manager (ASAM) → Installed Packages → Update All…
Now ‘Delete…’ any obsolete packages.

I got another error saying that there are packages dependent on adb, but it isn’t running. I think the 0.9.8 SKD might be fubar. After more research, there is a problem with Ubuntu 64bit (which I’m running). It looks like some 32bit libraries need to be installed. There was nothing explicit on the Android SDK site, but after more searching I found references to the 32 bit libraries under an Ubuntu forum. Then I found a matching troubleshooting article on the SDK site.

$ sudo apt-get install ia32-libs
$ /opt/android/latest/tools/adb 

Yay! ADB now does something! That was the root of my problem. Now everything is right with the world. Two hours later my quest continues.

Ubuntu Linux 13.10 64-bit
The error ‘bash: ./adb: No such file or directory’ came back. But this time ‘install ia32-libs’ did not work. However, this seemed to do the trick:

$ sudo apt-get install libc6:i386 libstdc++6:i386

Hello World
Follow the instructions from the Android Site. http://developer.android.com/resources/tutorials/hello-world.html
After the errors before, getting the Hello, Android app running was a piece of cake. That emulator is pretty cool. Then I said, f@#$ it, and I set it up to run on my windows environment.

Posted in Programming | Tagged: , | 1 Comment »

Postgres 8.3, JDBC, JPA, and Hibernate example setup.

Posted by jaystile on April 1, 2009

I wanted to access my local postgresql database with JDBC and JPA in the J2SE environment. Refer to my previous post about setting up Postgres 8.3 on Ubuntu 8.04.

If you can’t tell, I’m using java. I’m compiling to java 5. I’m building my project with maven. Inside of a larger maven project which creates my ear file, I created a test project ‘runner’. This creates a jar.

<project> <modelVersion>4.0.0</modelVersion> <groupId>org.myproject</groupId> <artifactId>runner</artifactId> <packaging>jar</packaging> <name>JAR Runner</name> <version>1.0</version> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencies> <dependency> <groupId>org.myproject</groupId> <artifactId>util</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>8.3-603.jdbc4</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>ejb3-persistence</artifactId> <version>1.0.2.GA</version> </dependency> <dependency> <!-- Note this added other dependencies - hibernate-commons-annotations-3.1.0.GA.jar - slf4j-api.1.4.2.jar - exclude this because it is bunk and causes exceptions. - hibernate-annotations-3.4.0.GA.jar - antlr-2.7.6.jar - commons-collections-3.1.jar - dom4j-1.6.1.jar - xml-apis-1.0.b2.jar - jta-1.1.jar - javassit-3.4.GA.jar --> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager </artifactId> <version>3.4.0.GA</version> <exclusions> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusions> </dependency> <!-- The org.slf4j needed to be included because the one included in org.hibernate.ejb3-persistence did not work --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> </dependencies> </project>

runner/src/main/resources/META-INF/persistence.xml file
The core of JPA uses the persistence.xml file to set up the database and entity mappings. I tried using Sun’s example but I found it to hard to configure. I know that hibernate provides a JPA implementation so I used that instead of the glassfish example. It was easy to add because it exists in the maven 2 repository. This persistence.xml is configured to talk to the postgresql 8.3 database. It also contains a mapping for my Entity.

<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL"> <class>org.myproject.runner.MapEntity</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <!-- <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> --> <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> <property name="hibernate.connection.username" value="myuser"/> <property name="hibernate.connection.password" value="mypassword"/> <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/mydb"/> <property name="hibernate.max_fetch_depth" value="3"/> <!-- cache configuration --> <!-- This was blowing up -v <property name="hibernate.ejb.classcache.org.hibernate.ejb.test.Item" value="read-write"/> <property name="hibernate.ejb.collectioncache.org.hibernate.ejb.test.Item.distributors" value="read-write, RegionName"/> --> <!-- alternatively to <class> and <property> declarations, you can use a regular hibernate.cfg.xml file --> <!-- property name="hibernate.ejb.cfgfile" value="/org/hibernate/ejb/test/hibernate.cfg.xml"/ --> </properties> </persistence-unit> </persistence>

Our database is populated by some sql.

create table map ( pk_id int unique primary key, width int, height int, name varchar(256), last_updated timestamp ); GRANT ALL ON map TO PUBLIC; INSERT INTO map (pk_id, width, height, name, last_updated) VALUES (1, 100, 200, 'test_map', CURRENT_TIMESTAMP);

Next, we have an entity to use against our database. It only maps one field, but this was just for testing purposes.

package org.myproject.runner; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="MAP") public class MapEntity { private long id; @Id @Column(name="PK_ID") public long getId() { return id; } public void setId(long id) { this.id = id; } }

Finally, we have a java class that can call the database via JDBC or JPA

package org.myproject.runner; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class DatabaseAccess { /**
  • @param args
  • /
public static void main(String[] args) { testJdbc(); testJpa(); } public static void testJdbc() { String url = "jdbc:postgresql://localhost/mydb"; Properties props = new Properties(); props.setProperty("user","myuser"); props.setProperty("password","mypassword"); Connection conn = null; try { conn = DriverManager.getConnection(url, props); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from map"); while(rs.next()) { long id = rs.getLong(1); System.out.println("ID:" + id); } } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void testJpa() { // Use persistence.xml configuration EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1"); EntityManager em = emf.createEntityManager(); // Retrieve an application managed entity manager // Work with the EM MapEntity map = new MapEntity(); map.setId(5); EntityTransaction transaction = em.getTransaction(); transaction.begin(); em.persist(map); transaction.commit(); em.close(); emf.close(); //close at application end // Create EntityManagerFactory for persistent unit named "pu1" // to be used in this test } }

Posted in Programming | Tagged: , | 4 Comments »

Perl: Converting Decimal to Degrees Minutes Seconds (and vice-versa)

Posted by jaystile on February 21, 2009

Here is some code I playing with trying to get conversion from one coordinate system to another. There is some percision loss due to the floating point math in the conversions. So expect to see a loss of a few seconds when converting from decimal.

1 #!/usr/bin/perl
3 use strict;
4 use POSIX qw(ceil floor);
6 # flush print to the output after every write.
7 $| = 1;
9 testConvertDegMinSecToDecimal();
10 testConvertDecimalToDegMinSec();
13 sub testConvertDegMinSecToDecimal {
14 my $result = convertDegMinSecToDecimal(121, 8, 6);
15 print "RESULT: $result\n";
17 my $result = convertDegMinSecToDecimal(121, 59, 59);
18 print "RESULT: $result\n";
19 }
21 sub testConvertDecimalToDegMinSec {
22 my $deg; my $min; my $sec;
23 ($deg, $min, $sec) = convertDecimalToDegMinSec(121.135);
24 print "RESULT - DEG: $deg MIN: $min SEC: $sec\n";
26 ($deg, $min, $sec) = convertDecimalToDegMinSec(121.99317);
27 print "RESULT - DEG: $deg MIN: $min SEC: $sec\n";
28 }
31 sub convertDegMinSecToDecimal {
32 my $deg = $_[0];
33 my $min = $_[1];
34 my $sec = $_[2];
36 my $result = $deg + ($min/60) + (($sec/60) *(1/60));
37 $result = sprintf("%.5f", $result);
38 }
40 sub convertDecimalToDegMinSec {
41 my $input = $_[0];
42 my $deg = floor($input);
44 $input = $input - $deg;
45 $input = $input * 60;
47 my $min = floor($input);
49 $input = $input - $min;
50 $input = $input * 60;
52 my $sec = floor($input);
54 return ($deg, $min, $sec);
55 }

If you’re playing with degrees minutes seconds & decimal degree conversions you’re probably interested in calculating distance on the Earth. There are two common algorithms for calculating distance that are available as CPAN modules:

Posted in Programming | Tagged: , , | Leave a Comment »

Ubuntu: Howto Setup a Local Subversion Repository

Posted by jaystile on February 13, 2009

This Howto is targeted for Ubuntu 8.04, Hardy Heron. Specifically, this will go through a quick start to get subversion running on the file system. Which, by the way, is NOT the recommended way of setting it up. But if you’re doing some local (1-person) development it is the quickest. The documentation for subversion can be found at svnbook.red-bean.com

Get Subversion!
I used the Synaptec Package Manager to download the ‘subversion’ package. You could probably done just as well using ‘apt-get’.

Create Repository!

svnadmin create /var/svnroot/

Import your code into the repository!
Where LocalDir is like /home/username/MyProject
and Where ProjectName is like MyProject

svn import LocalDir file:///var/svnroot/ProjectName

Check out your code from the repository!

svn checkout file:///var/svnroot/PorjectName

Your code is now under version control! (Don’t you feel all grown up now?)

Posted in Programming | Tagged: , | Leave a Comment »

Want to remember forever?

Posted by jaystile on December 2, 2008

Have you heard of this guy, Piotr Wozniak? I first read about him in an article at wired.com. He has been researching memory since 1985 and has developed an algorithm to help you learn things until your mental faculties completely leave you.

Excerpts from the Wired article
Say you have learned something…
Practice too soon and you waste your time. Practice too late and you’ve forgotten the material and have to relearn it. The right time to practice is just at the moment you’re about to forget. Unfortunately, this moment is different for every person and each bit of information.

When do we forget something we have learned?
….human forgetting follows a pattern. We forget exponentially. A graph of our likelihood of getting the correct answer on a quiz sweeps quickly downward over time and then levels off.


SuperMemo is a tool that has been created to implement Wozniak’s algorithm to test people at a specified periods to ‘refresh’ their memory of a subject. From looking at screenshots and website the software leaves much to be desired, but the idea is awesome! It has been geared toward learning languages, but there a lot more possibilities! Imagine a tracking system for school aged children that repeatedly tests their math skills or grammar skills. You would have customized tests based on what the child can recall and have a very precise idea of what skills the child has mastered. A SuperMemo package could be used for medical students with the huge amounts of data that need to be recalled. This would also apply for physics, chemistry, and math. I’m thinking about trying it myself to learn all the odds for my Poker Hands.

Has anyone used this product? I found one account of usage that seemed promising where they were studying for an Oracle DBA exam.

Posted in Cool Stuff | Tagged: , , , | 1 Comment »