Results 1 to 11 of 11
  1. #1
    tux008's Avatar
    tux008 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    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, 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 the application runs perfectly, on both systems.

    The source-code is:

    Java Code:
    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 JosAH; 07-11-2011 at 11:03 AM. Reason: added [code] ... [/code] tags

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Suggests that the jar is being executed as root. What happens when you run it from the command as a non root user?

  3. #3
    tux008's Avatar
    tux008 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Strangely, things go like this for non root users:

    No protocol specified
    Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$100(X11Graph icsEnvironment.java:52)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEn vironment.java:155)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11Graphic sEnvironment.java:131)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvir onment(GraphicsEnvironment.java:68)
    at java.awt.Window.init(Window.java:380)
    at java.awt.Window.<init>(Window.java:433)
    at java.awt.Frame.<init>(Frame.java:403)
    at java.awt.Frame.<init>(Frame.java:368)
    at javax.swing.JFrame.<init>(JFrame.java:158)
    at SystemProperties.<init>(SystemProperties.java:19)
    at SystemProperties.main(SystemProperties.java:15)

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That confirms that you were running as root all along and explains why you always got /root as the user.dir.

    To get the other users to be able to use X you can try
    Java Code:
    export DISPLAY=":0.0"
    in their profile. The exact details of how to allow non root users access to your X are ofcourse distro specific and have nothing to do with Java.

  5. #5
    tux008's Avatar
    tux008 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    I added in /home/user/.bash_profile file export DISPLAY=":0.0", but I still get the same java.lang.InternalError, but only if I login as root and I open a terminal, changing the current user to a non root user, in my case named "user". More, if I login as non root (user), all things go smooth, there is no java.lang.InternalError, except it now displays (by double-clicking the jar file) user.dir = /home/user. If I launch it by command-line, user.dir has a normal value, corresponding to the true current working directory.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If you login as root, double clicking will run the program as root. That is normal expected behaviour.
    When you login as root and change the user on the terminal, the .bash_profile settings may not have been applied. You may need to execute them manually.
    I don't see why you would want to log in as root when you want to get user's behaviour.

  7. #7
    tux008's Avatar
    tux008 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    I may not want to get user's behaviour necessarily, but the problem of different user.dir values between command-line launch and GUI launch still persists.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by tux008 View Post
    I may not want to get user's behaviour necessarily, but the problem of different user.dir values between command-line launch and GUI launch still persists.
    I don't understand. You said above that

    More, if I login as non root (user), all things go smooth, there is no java.lang.InternalError, except it now displays (by double-clicking the jar file) user.dir = /home/user. If I launch it by command-line, user.dir has a normal value, corresponding to the true current working directory.
    When you run from the comand line, what directory(pwd) are you at that is giving different results from double clicking ?.

  9. #9
    tux008's Avatar
    tux008 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    The problem is that I get the same user.dir value on GNU/Linux for the application when I launch it by double-clicking the JAR executable. If I login with different users it always displays the value of user.dir as their corresponding home directories (for the root user - /root, for the user user - /home/user, etc), just as if the JVM makes a mistake between user.dir and user.home. But all runs well - that is, displays the correct user.dir - if I launch it from command-line. That's the strange thing. On Windows XP the value of user.dir is the same, regardless of the executable's location and the method of launching the application.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Just a thought, but on my Ubuntu K desktop the double click behaviour is to open the jar file like any other archive. Only on Windows do I see the javaw.exe behaviour.

    So I am wondering what executable gets invoked when you double click on the icon representing a .jar file. Perhaps it cds into ~ and then executes java -jar <full path to jar file>. In that case you would always see user.dir reported as the same as user.home.

  11. #11
    tux008's Avatar
    tux008 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thank you, pbrockway2!

    The command associated with the JAR files is indeed java -jar. The only problem now would be how to mimic the behaviour of javaw.exe on GNU/Linux since there is no javaw executable for the GNU/Linux version of JDK.

    Wrong current working directory-img.jpg

Similar Threads

  1. changing current directory
    By flaca in forum New To Java
    Replies: 7
    Last Post: 03-06-2011, 02:50 AM
  2. Static function to get current working directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-23-2008, 02:13 AM
  3. How to get Current Directory through File
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:14 AM
  4. How to get the current working directory in EJB?
    By sathish_2111 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-19-2007, 04:24 PM
  5. How can i get current directory?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •