Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Wrong current working directory

  1. #1
    Junior Member tux008's Avatar
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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);
        }
    }
    Last edited by helloworld922; July 11th, 2011 at 08:13 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Wrong current working directory

    Is this the same post?
    Wrong current working directory

Similar Threads

  1. Replies: 0
    Last Post: April 15th, 2011, 03:51 PM
  2. [SOLVED] how to get each field of a current date
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 24th, 2010, 05:33 AM
  3. how to add a new entry in a current array
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 28th, 2009, 05:39 PM
  4. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM