Monday, March 21, 2016

Fix Unsupported major.minor version

Source: stockoverflow

Sometimes when you build your project with wrong java version and run your project, you get this kind of error, below is a guide to what java version is needed when this error occurs...

The version number shown describes the version of the JRE the class file is compatible with.
The reported major numbers are:
J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

Source: stockoverflow postgres
If you encounter this error, PostgreSQL unsupported major.minor version
java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0 (unable to load class org.postgresql.Driver)

This means that the postgres driver you're using does not support the java version used to build your project.

You can check here in postgres documentation the jdbc version you need if you're running a certain java version: https://jdbc.postgresql.org/download.html

Below are the details found in this page...

If you are using the 1.6 then you should use the JDBC4 version. If you are using 1.7 then you should use the JDBC41 version. If you are using 1.8 then you should use the JDBC42 versionIf you are using a java version older than 1.6 then you will need to use a JDBC3 version of the driver, which will by necessity not be current

Monday, October 26, 2015

Updating to El Capitan and suddenly Maven is not working

I'm using IntelliJ IDE and when I updated to El Capitan version of OSX, I started getting this irritating pop out error in the IDE "can't start git: /usr/bin/git". I looked for a solution, and then I found this website: JetBrains Support

The solution is basically to install xcode (if you still don't have it) and agree to the licenses by running "sudo xcodebuild -license" in your terminal.

So, everything works well, I should be able to work properly the next day. And when I tried running my Selenium WebDriver scripts, my JAVA_HOME can't be found by Maven! So I need to search for a way to define my JAVA_HOME with minimal updates to the system (that's always the best way so you won't get confused when you want or need to update it).

I found this thread in stackoverflow.com and below is the solution that I did.

  1. In your home directory: (/Users/{username})
  2. nano .bash_profile and add the line below :
    export
    JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home`
  3. Make sure the path "/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home" exist.

Wednesday, August 26, 2015

SSH using your Mac Terminal

As a windows user, I'm used to using PPK keys and putty to SSH. But with mac osx, I don't have to download any software and just SSH in my terminal.

With mac, you'll be needing a .pub file. I tried converting my PPK in windows to mac but so far I have not found a good way to convert it, I'm having problem with passphrase. For some reason it's not accepting the right passphrase.

I found a good blog  that explains how to create key and clone your git repository: using-git-and-github-on-os-x and a blog post in atlassian ssh config

If you simply one to create key, then you can just follow Step 1 and 2:
Open your terminal and...
1) Go to .ssh folder ( usually located in ~/.ssh )
2) ssh-keygen

Now for you to be able to successfully SSH, you need to know 2 more commands before starting your ssh...

SSH CommandPurpose
ssh-keygenCreates key pairs.
ssh-agentAgent for providing keys to remote servers. The agent holds loaded keys in memory.
ssh-addLoads a private key into the agent.

You need to make sure that ssh-agent is running because it's the one holding the keys that you're going to load to the remote server you're going to connect to.

To run it, just simply type: eval ssh-agent
in your terminal.

Then you need to load the keys you have in your .ssh folder: ssh-add
If you want to specifically add a key, you can add the location of the key after ssh-add <key_location>

To verify if it's all loaded, just do: ssh-add -l

Now, you can finally ssh you your remote server: ssh <remote server ipaddress or domain>

Next time you need to connect, you can just run the agent together with your ssh command by doing this: ssh -A <remote server address> or if they key can't be found for some reason you can try calling the key too ssh -A -i <yourkey_loc> <remote server address>

I hope this helps. :)

Monday, May 25, 2015

Debugging Maven Project (IntelliJ IDEA)

How to debug your Maven test project?

Well, the command can be found here...
http://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html

But the next question is always... "How can I apply it in my IDE?" It's easy to apply it in command line, but of course, you want to be able to debug your project in an IDE. :)

So we'll use this command and add a configuration in IntelliJ
mvn -DforkCount=0 test

In your IntelliJ go to Run > Edit Configurations
Then add a new maven configuration, by clicking the plus sign in the upper left corner.

There are 4 tabs available: Parameters, General, Runner, and Logs

In Parameters tab:
  • Working Directory: <Is your project directory>
  • Command Line: test
  • Profiles: <blank, unless you have a profile>
Go to Runner tab
  • VM Options: -DforkCount=0
  • JRE: Use the jdk version of your project
Click Apply, then Ok.

After this, you now can debug your Maven test project.

So for other configurations, if you want to add more parameters, you know now where to go and add those parameters. All command of maven should be on Command Line, while parameters goes in parameters tab. ;)
I hope this helps!

Wednesday, May 20, 2015

Maven Project in Mac OSX & Importing in IntelliJ

For the sake of not repeating the tutorials that are already available online, and they are good tutorials if you will take time and read it. I'm going to list down the helpful links first before writing down the things I've done to create my Maven project in Mac and converting my java project to a "mavenized" one.

  • http://www.mkyong.com/tutorials/maven-tutorials/
  • http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
  • Combine the resources and tutorial you'll find in these 2 links and you should be able to understand what Maven is and why create a project in Maven.
Pre-requisite: Installed Maven in your machine, how to know if you have maven? mvn -version
If it didn't show any maven version, then you have no maven installed.

I'm using IntelliJ Community Edition 14. I'm using this instead of eclipse because when my friend was having a problem with pom.xml, eclipse didn't return any error but just compiled the project without returning any error. On the other hand, IntelliJ returned the error about the pom.xml and my friend was able to proceed with the project. Since then, we've been using IntelliJ in creating our Selenium Webdriver scripts.

You can actually create a maven project straight from IntelliJ, but I won't go through that since that's pretty much documented in IntelliJ's website. So I'll be documenting the steps I did to create a maven project, import it in IntelliJ and then transfer all my java classes from Java project to a Maven project.

  1. Run your terminal / console
  2. Go to the directory where you want your maven project created
  3. Generate maven project: Type this to your terminal without quotes "mvn archetype:generate"
When you press enter, this command you type is not complete to create a maven project, so maven will have to ask you some things first before it can proceed.

It'll ask you a number or filter you want to apply, something like this...
$ Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 604: 

If you know the number you want for the project, then type away. But if you don't know, you can simply copy the number it's showing. So in this case, the number 604. Based on my understanding, and what happened after I've created the project, the number that was shown was the archetypeID for a quickstart project in maven.

Then you'll be asked for the following details too...

Group ID: package where you want your class to run (example: org.company.bank)
Artifact ID: this is your project name (example: TestBank)
Snapshot 1.0: version of your maven, you can press enter and leave it blank or put the desired number
Package: you can leave it blank if you want your group id as your package already

Then to finish, enter "Y" to proceed, or "N" if you want to update/change something.

And all these steps can all be summarized to this one liner code...
$ mvn archetype:generate -DgroupId=org.company -DartifactId=TestBank -Dversion=1.0-SNAPSHOT  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  • Run your IntelliJ and click on File > Import Project
  • Choose or look for your maven project and click OK.
  • Choose Maven and click next.
  • If you have any specific project settings you need to adjust, you can do it now, or do it later. Click next.
  • Then select your maven project and click next.
  • Choose the SDK version the project will use
  • Finally, the project name and click finish.
Now when you open your project, you might need to double check if your Maven in IntelliJ is pointing to the right location of maven. You can choose to override it and point it to your Maven's home. You can find where your maven is located when you do the mvn -version in ternminal. You can check with IntelliJ how to look for maven properties.

Now, next major thing you have to sort or fix is the pom.xml, why? Because maven needs the settings declared in pom.xml to build / run the whole project. Without proper pom.xml, your maven project won't run.

What's the quickest way to understand the pom.xml? Think of all your libraries or modules settings in your java project that you have to setup before compiling your project, those libraries have to be in your maven project too. But wait! You don't do it like in java project, because remember it's Maven project. So you have to use the pom and declare dependency to each libraries you have placed or used in your java project.

Quick example in my case...
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.modirum.generic.webdriver</groupId>
  <artifactId>Selenium-Webdriver-3ds</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Selenium-Webdriver-3ds</name>
  <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testing.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
As you can see here, you can get all these dependencies in http://mvnrepository.com/
You search for your library or jar, you click the project, click the latest version and then copy paste the dependency to your pom.xml

You'll see testng 6.9.4, log4j 1.2.17, and selenium-java 2.45.0
Then I have a surefire plugin to execute my testing.xml suite file. The plugins are not necessary if you don't need it yet, but like in my case, I've been using testing.xml in my java/testng project, so I need to add a plugin that will allow me to call testing.xml.

And every time you delete or add dependency, you'll notice that maven downloads the library immediately and stores it in your local maven repository.

You'll see your IDE loading whenever you add or remove a dependency. Initially the version of the dependency will be marked as red, that only means it's not yet available in your local repository, but once it's downloaded the red color of the font will become black, signifying that the download was successful.

And to test or run your maven project, you have to go to Run > Edit Configurations
and configure a maven command. For example, I want to run "mvn test" I'll have to create this setup...
  • Type a run config name: maven test
  • Working Directory: <my maven project>
  • Command line: test
  • Profiles: <blank>
  • Click apply, then run.
Then one last step is importing your java tests files to maven.
In maven you have to follow certain file structures or directory layout, you should have seen it, because it's one of the link I mentioned above.

The src/main -> is basically where all your application and files related to application should go
while src/test -> is where our test web driver scripts should go

Why is this structured this way? As much as possible, maven wants to standardized the structure of the project. The archetype that was mentioned earlier are like templates for the kind of project you want your maven structure to pattern with. So, just setup the package you need under src/test and then copy and paste the java files to the right directory / package.

With IntelliJ, you don't have to one by one open your files and change the package name from your old java project to the new one, because it can do that for you. So all you have to do is copy, paste, wait for IDE to finish, and viola! You now have a "mavenized" project!

Typically when you do your first mvn test, you get some errors, but that is if you've missed some things like path to your csv or xls file. Or you have missing dependency in pom.xml and so on... but if all is good, it should run. So learn to read the error it's throwing, so you know what's the problem. And most likely the error you'll encounter has been encountered by other people so you'll find the answer when you google it. ;)

I hope this helps, and have fun with your project! :D

Friday, February 6, 2015

Mac Terminal (Git / Maven / Path)

TO INSTALL AND CLONE A GIT REPO Download Git from http://git-scm.com/downloads Go to Terminal and type
$ git --version
There are times that you'll be asked to install the dev tools for command line, so just proceed with it. Clone Remote Git Repo git clone -b
$ git clone -b my-branch git@github.com:user/myproject.git
TO INSTALL MAVEN Download latest maven: http://maven.apache.org/download.cgi Follow the instructions under Unix-based OS (Linux, Solaris, and Mac OSX) If you want to go to /usr/local then you have to move the extracted maven folder using the terminal. Below are sample lines in moving the file
$ sudo mv -f /Users//apache-maven-3.2.5 /usr/local/
**replace to your actual user name, only if that's the location where you've extracted your maven; if not then just type the path to that location **I need to use sudo for the system to allow me to move the file to /usr/local Now I need to add apache maven bin to the paths. If I type "echo $PATH" the maven is still not included. I've edited PATH by
$ sudo nano /etc/paths
While editing the paths you'll find symbols like this ^X below, together with other options. This means Control+X (not Control+Shift+X). And for detailed explanation of java_home in mac, I find this website from apple developer really helpful, Apple Developer Q&A Java_Home. Once you've saved your paths, you should test if maven is properly installed by typing
$ mvn --version
if it does not work, try closing all your terminals and running it again. Then it should work, especially if maven path is showing already in PATH. Use "$ echo $PATH" to show all listed paths.

Sunday, January 25, 2015

SQL Notes: Problem using LIMIT in your code?

I want to get only the first row of my select SQL.
Why? Because I wanted to get the max of the alpha numeric document number system.
The best way to do that (but this depends on the pattern) to a document number with good pattern like ABCXXXXX01 (X stands for digits), is to order it by DESC.

Problem is it's going to return a bunch of results, so instead of creating another method just to do that, why not limit the result to 1? The only problem is I'm ecountering this LIMIT error in PostgreSQL. So I have to find another way to fetch this 1 row.

So below are other ways to get that 1 row (data below are from: freedb2)
For example, in Microsoft SQL Server you would use TOP:
SELECT TOP 10 column FROM table
MySQL and PostgreSQL SQL would use LIMIT like so:
SELECT column FROM table LIMIT 10
PostgreSQL v8.3 and later can also use this more standard SQL:
SELECT column FROM table FETCH FIRST 10 ROWS ONLY
An Oracle programmer would write
SELECT column FROM table WHERE ROWNUM <= 10
In Sybase, you would set rowcount
SET rowcount 10
SELECT column FROM tablez
So obviously, what worked for postgres is FETCH FIRST instead of LIMIT.
Now, I'm good. I've used this sql in Adempiere code. :)
Just spreading this good documentations from FreeDB2.