I created some illustrative and simple implementations of common Unix commands. For those who are familiar with Unix-like systems them make easier to understand Java. For those who are familiar with Java them make easier to understand Unix-like systems. 🙂
1. PWD
The first one is pwd that show the current working directory.
public class Jpwd {
public static void main(String[] args) {
String pwd = System.getProperty("user.dir");
System.out.println(pwd);
}
}
Running this at /home/silveira directory gives us as output:
$ java Jpwd
/home/silveira
1. CAT
The command cat is usually utilized for displaying files.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Jcat {
public static void main(String[] args) {
if(args.length==1){
try {
FileReader fileReader = new FileReader(args[0]);
BufferedReader in = new BufferedReader(fileReader);
String line;
while((line = in.readLine())!= null){
System.out.println(line);
}
} catch (FileNotFoundException ex) {
System.out.println(args[0]+", file not found.");
}
catch (IOException ex) {
System.out.println(args[0]+", input/output error.");
}
}
}
}
$ java Jcat /etc/timezone
America/Fortaleza
3. LS
The command ls is to list files. The File API (java.io.File) is very flexible and portable, but in this example I want just list files and directories of the current directory.
import java.io.File;
public class Jls {
public static void main(String[] args) {
File dir = new File(System.getProperty("user.dir"));
String childs[] = dir.list();
for(String child: childs){
System.out.println(child);
}
}
}
Usage:
$ java Jpwd
/home/silveira/example
$ java Jls
directoryA
fileA
.somefile
4. CD
The cd command changes the current working directory.
import java.io.File;
public class Jcd {
public static void main(String[] args) {
if(args.length==1){
File dir = new File(args[0]);
if(dir.isDirectory()==true) {
System.setProperty("user.dir", dir.getAbsolutePath());
} else {
System.out.println(args[0] + "is not a directory.");
}
}
}
}
One thing that I like a lot to do with JavaFX is draggable objects. Due to some recent changes in the JavaFX syntax my old codes for that are no longer working. Joshua Marinacci from Sun’s JavaFX engineering team and other guys from the JavaFX community gave me some tips. Here some strategies I’m using for making draggable nodes in JavaFX.
In this first example, a simple draggable ellipse.
When you need to create a group of draggable objects, you can try thie approach of a draggable group like this. Inside on it, you can put whatever you want.
Inkscape is a Open Source vector graphics editor that works with SVG (Scalable Vector Graphics) format, Inkscape works with transparency, gradients, node editing, pattern fills, PNG export, and more. It also runs on Linux, Windows and OSX, those three are officially supported, but also runs in a broad list of Operational Systems. Is a software that I work daily and frequently is featured here in my blog.
You can download Inkscape or directly install it via some package system like Apt:
sudo apt-get install inskcape
But sometimes we need some special feature that is not available yet in the repositories or we want gain speed by having special binaries for our platforms or we want to help developing a new feature. In those cases we need to compile the software by ourself.
Those tips are valid for Ubuntu 8.04 but some part of them can be applied in others distributions. The Inkscape compiled here is the version 0.46+devel so newest versions can have compiling procedures slightly different.
Getting sources via APT.The easiest way to compile Inkscape on Ubuntu is
sudo su
apt-get build-dep inkscape
apt-get source inkscape
cd inkscape
./autogen.sh
./configure
make
make install
This will get a version of inkscape, compile it and install. If the first step doesn’t work well, you can try install all necessary packages by yourself using:
Getting sources via SVN. The recipe I showed above will compile a stable version of Inkscape but not the last version of Inkscape. For that we need to grab the source directly from the Subversion repositories and so compile it.
A alternative way to subversion is getting sources from here. Those are tarballs built every hour after someone change something in the development repositories. Download a tarball, and decompress it on your home folder.
Install all tools we need to compile Inkscape, this should fits:
Enter in the directory with the Inkscape source and do:
./autogen.sh
mkdir build
cd build
../configure
make
sudo make install
In both cases, grabbing sources via svn or via apt, or can set the place where the software will be installed so it not cause conflicts with you already installed version of Inkscape. You can do that replacing the ./configure step with something like:
./configure –prefix=/home/yourname/inkscape
If you had some trouble in one of those steps, consider reading some of those other tutorials:
class Talker
def method_missing(method)
if method.to_s =~ /say_/
puts $'
end
end
end
t = Talker.new
t.say_hello
t.say_something
t.say_goodbye
And running
$ ruby Talker.rb
hello
something
goodbye
But why do that when we have parameters? Because you can do some methods really dynamic with a clear semantic, just looking for them you know are they are doing.
public class PreventTimeTravelers {
public static void main(String args[]){
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
assert endTime >= startTime: "We came back in time!";
}
}
Please do not execute this code in a time machine.
NumberFormatException: Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
An simple example:
public class SummationExample {
public static void main(String args[]){
int sum = 0;
for(String arg: args){
System.out.println("+" + arg);
sum += Integer.parseInt(arg);
}
System.out.println("= " + sum);
}
}
$ java SummationExample 1 2 3 4 five
+1
+2
+3
+4
+five
Exception in thread “main” java.lang.NumberFormatException: For input string: “five”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at SummationExample.main(SummationExample.java:6)
So
public class SummationExample {
public static void main(String args[]){
int sum = 0;
for(String arg: args){
try {
sum += Integer.parseInt(arg);
System.out.println("+" + arg);
} catch (NumberFormatException e) {
// nothing
}
}
System.out.println("= " + sum);
}
}
import java.util.Properties;
public class PropertiesLister{
public static void main (String args[]){
Properties props = System.getProperties();
props.list(System.out);
}
}
In the machine I’m writing right now:
— listing properties —
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=/usr/lib/jvm/java-6-sun-1.6.0.00/jre/…
java.vm.version=1.6.0-b105
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=:
java.vm.name=Java HotSpot(TM) Server VM
file.encoding.pkg=sun.io
user.country=BR
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/tmp
java.runtime.version=1.6.0-b105
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
java.endorsed.dirs=/usr/lib/jvm/java-6-sun-1.6.0.00/jre/…
os.arch=i386
java.io.tmpdir=/tmp
line.separator=
java.vm.specification.vendor=Sun Microsystems Inc.
os.name=Linux
sun.jnu.encoding=UTF-8
java.library.path=/usr/lib/jvm/java-6-sun-1.6.0.00/jre/…
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Server Compiler
os.version=2.6.20-16-generic
user.home=/home/export/silveira
user.timezone=
java.awt.printerjob=sun.print.PSPrinterJob
file.encoding=UTF-8
java.specification.version=1.6
user.name=silveira
java.class.path=.
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=/usr/lib/jvm/java-6-sun-1.6.0.00/jre
java.specification.vendor=Sun Microsystems Inc.
user.language=pt
java.vm.info=mixed mode
java.version=1.6.0
java.ext.dirs=/usr/lib/jvm/java-6-sun-1.6.0.00/jre/…
sun.boot.class.path=/usr/lib/jvm/java-6-sun-1.6.0.00/jre/…
java.vendor=Sun Microsystems Inc.
file.separator=/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport…
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.cpu.isalist=