eTechtips

Tech Tips and Insights

Category: Programming

  • Gallery of Hello World!

    Here is a list of the Hello World Programs for different languages:

    C

    #include <stdio.h>
    int main()
    {
    printf("Hello World!");
    }

    C++

    #include <iostream.h>
    int main()
    {
    cout << "Hello World!" << endl;
    }

    C#

    public class HelloWorld
      public static void Main()
      {
        System.Console.WriteLine("Hello World!");
      }

    Java

    class HelloWorld {
    static void main(String[] args)
    {
    System.out.println("Hello World!");
    }

    SHELL

    echo "Hello World"

    Python 2

    print "Hello World!\n"

    Python 3

    print ("Hello World!")

    Ruby

    puts "Hello World!"

    Perl

    print "Hello World!n";

    PHP

      <?php      
        print "Hello World!";
      ?>

    Rust

    fn main() {
        println("Hello World!");
    }

    This is the simplest form of Hello World for most of these languages.

    0 Comments
  • Using tar over ssh

    Every once in awhile, I need to backup a large directory from another computer. This can be done by creating a zip or tar of the files you are looking to save and then copying those zips to the other computer. But what it you have a very large directory and not enough room to store the extra file. Here is a way to use tar over ssh:

    ssh tar zcvf – /wwwdata | ssh root@backupserver.mydomain.com “cat > /backup/wwwdata.tar.gz”

    0 Comments
  • Setting timestamp with Touch

    Did you know? You can Set file timestamp using touch command. One way to do this is:
    touch -c -t YYMMDDhhmm filename.txt

    0 Comments
  • Retrieve drive letter on Windows

    The other day I had to write a script to change to a working directory and back to the original directory. The problem was they were on different drives on Windows. So I had to find a way to capture the drive letter
    and switch back to that drive. When using the following command,

    cd F:somedir
    

    you are not moved to the “F:” drive without an explicit “F:” command.

    The cd Command on windows will show the current directory location. Using some string manipulation on that and capturing it to a variable allows us to go back to that drive.

    REM Capture the current drive letter
    set ORIGDRIVE=%cd:~0,2%
    
    F:
    REM some other scripting things here
    
    REM Return to the original drive letter
    ORIGDRIVE
    

    The first line captures the current drive letter by specifying the first character with 0 and the ,2 states to pick up the next 2 characters which would hold the “C:” or whatever drive you started on.

    0 Comments
  • Utility: Find files files older than a certain amount of days

    I had to write a backup script the other day and used the find command to find the files that were older than a given amount of days.

    DAYS_TO_KEEP=7
    find /path/to/files* -type f -name *.bak -mtime $DAYS_TO_KEEP -exec rm {} ;
    

    The -name argument uses the escaped asterisk so command completion does not replace it with all the file names.

    The -mtime argument takes a number that is the number of days to look at.

    The -exec argument takes a command and the {} is replaced with the current file name and the ; is the end of the command.

    This is a small part of the power of the find command.

    0 Comments
  • Linux: watch command

    So every once in a while I want to see the progress of an upload or backup. So I turn to the watch command in Linux. The watch command gives the user the ability to re-run a command and see the updated output.

    The syntax is ‘watch -n -d .

    So for watching a file downloads progress you could run the following:

    watch -n 30 ls -l filedownloading

    This would show the ls output and you could watch the size of the file grow as it is dowload progress.

    0 Comments
  • Ant unable to find tools.jar

    When attempting to run ant I get the following message: “Unable to locate tools.jar. Expected to find it in C:Program Files (x86)Javajre6libtools.jar”

    This had to do with my JAVA_HOME not pointing to a Java JDK.

    So if you download a JDK and update your JAVA_HOME environment variable to point to that directory, ant should run happily.

    In BASH:
    export JAVA_HOME=/path/to/jdk

    IN DOS:
    set JAVA_HOME=c:PATHtojdk

    0 Comments
  • Sample Ant file

    Every once in awhile I go back and try to figure out how to create a basic ant build.xml file. So I am just placing a sample build.xml here.

    The directory structure should be something like
    ./build.xml
    ./src/HelloWorld.java

    The targets will create the build directory and compile the source and run a test execution of the class.

    <project name="HelloWorld" basedir="." default="main">
    <target name="clean">
    <delete dir="build"/>
    </target>
    
    <target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="build/classes"/>
    </target>
    
    <target name="jar" depends="compile">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
    <manifest>
    <attribute name="Main-Class" value="HelloWorld"/>
    </manifest>
    </jar>
    </target>
    
    <target name="run" depends="jar">
    <java jar="build/jar/HelloWorld.jar" fork="true" />
    </target>
    
    <target name="main" depends="clean,run"/>
    
    </project>
    
    0 Comments
  • Java Exception java.lang.NoClassDefFoundError and how to resolve it

    I had recently reinstalled my system and was trying to run a simple class that consisted
    of a “Hello World” program in Java. I received the following:

    Compile:

    $> javac LottoMain.java

    Run:

    $> java LottoMain

    Exception in thread “main” java.lang.NoClassDefFoundError: LottoMain
    Caused by: java.lang.ClassNotFoundException: LottoMain
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
    Could not find the main class: LottoMain. Program will exit.

    The compile line completed without errrors and the LottoMain.class file was there, but
    what I didn’t know was that I no longer had the current directory “.” in my CLASSPATH variable.

    After adding “.” to my CLASSPATH variable the run command gave me what I was expecting.

    Run:

    $> java LottoMain

    Hello Eric!

    Updating the CLASSPATH variable in Bash:
    in my home directory/.bashrc file

    export CLASSPATH=$CLASSPATH:.
    2 Comments
  • Java: Great link to Swing Examples

    Starting on my road to creating a Java based GUI I stumbled upon the following link within the “Creating a GUI with JFC/Swing” Train in the Java Tutorials from Oracle.

    Using Swing Components: Examples

    It has code examples and can really get you started on the road to a Java GUI interface.

    1 Comment