Wrong current working directory
Hello, everybody!
I have an unusual problem with a simple Java application that lists the Java system properties. The user.dir or current working directory is different when I run the application in GNU/Linux compared to Windows XP. In GNU/Linux, if I run it from the command line, it displays the right user.dir. But, if I run it by double-clicking the JAR executable (created by NetBeans 7.0 IDE) it displays always user.dir as ~ (/root, /home/user etc.), regardless of the executable's location. But this doesn't happen in Windows XP, where the application displays the same thing, no matter the choice made. If I launch the application from within the IDE, it runs perfectly, on both systems.
The source-code is:
Code Java:
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
class SystemProperties {
private JFrame f;
private JPanel p;
private JTextArea ta;
private JScrollPane sp;
public static void main(String[] args) {
new SystemProperties();
}
public SystemProperties() {
f = new JFrame();
p = new JPanel();
ta = new JTextArea(21, 28);
sp = new JScrollPane(ta);
sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
Properties props = System.getProperties();
Enumeration enum1 = props.propertyNames();
for (; enum1.hasMoreElements();) {
String propName = (String) enum1.nextElement();
String propValue = (String) props.get(propName);
if (propName.equals("java.library.path")) {
String[] libraryPath = propValue.split(":");
int length = libraryPath.length;
ta.append(propName + " = " + libraryPath[0] + "\n");
for (int i = 1; i < length; i++) {
ta.append(libraryPath[i] + "\n");
}
}
ta.append(propName + " = " + propValue + "\n");
}
File dir1 = new File(".");
File dir2 = new File("..");
try {
ta.append("Current directory is " + dir1.getCanonicalPath() + "\n");
ta.append("Parent directory is " + dir2.getCanonicalPath() + "\n");
} catch (Exception e) {
}
ta.setEditable(false);
p.add(sp);
f.add(p);
f.pack();
f.setTitle("System properties");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(400, 400);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((screenSize.width / 2 - f.getBounds().width / 2), (screenSize.height / 2 - f.getBounds().height / 2));
f.setVisible(true);
}
}
Re: Wrong current working directory