Category Archives: Development

Java – Appending objects to binary files

Java is tricky, you cannot add objects to already existent files with default classes.

Here is the solution:

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class Test{
    private static String filename = "test";

    public static void main(String[] args) {
        writeToBinary (filename, "a", true);
        writeToBinary (filename, "b", true);
        writeToBinary (filename, "c", true);
        readFromBinaryFile (filename);
    }

    public static void writeToBinary (String filename, Object obj, boolean append){
        File file = new File (filename);
        ObjectOutputStream out = null;

        try{
            if (!file.exists () || !append) out = new ObjectOutputStream (new FileOutputStream (filename));
            else out = new AppendableObjectOutputStream (new FileOutputStream (filename, append));
            out.writeObject(obj);
            out.flush ();
        }catch (Exception e){
            e.printStackTrace ();
        }finally{
            try{
                if (out != null) out.close ();
            }catch (Exception e){
                e.printStackTrace ();
            }
        }
    }

    public static void readFromBinaryFile (String filename){
        File file = new File (filename);

        if (file.exists ()){
            ObjectInputStream ois = null;
            try{
                ois = new ObjectInputStream (new FileInputStream (filename));
                while (true){
                    String s = (String)ois.readObject ();
                    System.out.println (s);
                }
            }catch (EOFException e){

            }catch (Exception e){
                e.printStackTrace ();
            }finally{
                try{
                    if (ois != null) ois.close();
                }catch (IOException e){
                    e.printStackTrace ();
                }
            }
        }
    }

    private static class AppendableObjectOutputStream extends ObjectOutputStream {
          public AppendableObjectOutputStream(OutputStream out) throws IOException {
            super(out);
          }

          @Override
          protected void writeStreamHeader() throws IOException {}
    }
}

 

References:

https://stackoverflow.com/questions/4646272/appending-objects-to-a-binary-file

maven – configuration for properties file

The desired requirements are the following:

  1. Output should be a JAR
  2. There must exist at least one properties file (e.g. config.properties). All properties files should not be placed inside the JAR, but should be easily accessible by the user.
  3. It should be easy to add more properties files without changing pom.xml

To address the requirements above we have to do the following steps:

  1. We create and place the properties file in src/main/resources
  2. To copy the properties file in the target directory upon build,  we add an include rule to the resources section.
  3. To create the JAR file, we use the maven-jar-plugin
    1. We should exclude properties files from the JAR build
    2. We should define the main class for the JAR so that it is executable
  4. We should copy the properties files from the resources directory to the target directory
  5. We should change the code to read the properties file from the classpath

To load the properties, we can use the following code:

Properties prop = new Properties();
prop.load(getClass().getResourceAsStream("/config.properties"));

 

Example of the pom.xml:

 <build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.7</version>
      <executions>
        <execution>
        <id>copy-resources</id>
        <phase>install</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target</outputDirectory>
          <resources>
            <resource>
            <directory>src/main/resources</directory>
            <includes>
              <include>**/*.properties</include>
            </includes>
            </resource>
          </resources>
        </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
        <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>com.blabla.MyMainClass</mainClass>
        </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

 

 

maven – Google maps dependencies

Sample dependencies for google maps in pom.xml:

<dependencies>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
   </dependency>
   <dependency>
       <groupId>com.google.maps</groupId>
       <artifactId>google-maps-services</artifactId>
       <version>0.1.20</version>
   </dependency>       
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-simple</artifactId>
       <version>1.6.2</version>
   </dependency>
   <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
       <version>2.2.4</version>
   </dependency>
   <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.7.0</version>
    </dependency>
</dependencies>

Netbeans C/C++ template with Valgrind and -Wall

Netbeans is a great free IDE, one I recommend to all beginners.

For C/C++ coding, it has one important drawback though: -Wall is not selected by default, which is something absolutely required for beginners.

For this reason, I have created my own template (-wall, run at external terminal, valgrind configuration etc) which is available here

In the following page you can find how to create and use one of your own:

https://blogs.oracle.com/seapegasus/entry/tip_create_your_own_project

 
Basically you point the IDE to an existing NetBeans project and tell it to use that as a template. You use a module project as a wrapper: This will allow you to turn the template project into an NBM file (“NetBeans Module”) that can be installed into the IDE like a plugin. After installing the custom plugin, the template project will appear in the “New Project” wizard. This is how you do it:

  1. Take a NetBeans project that you want to be the template project, and open it in the IDE. Let’s call it FooApp.
  2. Create an empty NetBeans Module project. Name it something recognizable (such as FooAppTemplate or whatever).
  3. In the Projects window, right-click the empty module project. Create a new file by choosing New > Other > Module Development > Project Template from the context menu.
  4. Use the Template Wizard to configure the template. Point it to the template project FooApp and choose a category (Java SE, Java EE, Java ME, Ruby, etc).
  5. Here comes the important part – you save the template as an NBM file. Right-click the module project in the Projects window, and select Create NBM. Look into the Output window to see where the file was saved (for instance FooAppTemplate/build/myorg-fooapptemplate.nbm).
  6. To install the template into the IDE, go to Tools > Plugins > Downloaded. Click Add Plugin, browse to your NBM file and install it. If you give the NBM file to other developers, that’s how they install the template.
  7. (Alternatively, right-click the module project in the Project window and select “Install in Development IDE”. This is another way to install it for somebody who has the module project still open.)

Install latest Qt to Ubuntu

  1. sudo mkdir opt/Qt         (notice Q has to be uppercase)
  2. Download open source Qt run file from official site
  3. execute .run file
  4. install at opt/Qt

Have more than 1 versions of qt?

sudo update-alternatives –install /usr/bin/qmake qmake /opt/Qt/ 10

Repeat for each version but with a different number.

To select which version to use as default, run:

sudo update-alternatives –config qmake

references:

http://jpnurmi.kapsi.fi/blog/2008/03/16/how-to-switch-qt-version-in-ubuntu/