This discussion is archived
3 Replies Latest reply: Apr 16, 2011 1:00 AM by Dude! RSS

Problem in creating log file on Ubuntu(Linux)

853982 Newbie
Currently Being Moderated
hi

I have developed a project in java. which creates log file for exception handling. This is working fine on Windows but When I run the jar file on ubuntu then it is not creating log file and throwing an error.

Error Message="Permission Denied UnixFileSystem.java createFileExclusively()"

Code is :
strStartupPath = new java.io.File("").getAbsolutePath();
DateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
String strFileName = strStartupPath + "\\" + dateformat.format(date) + ".log";
File logFile = new File(strFileName);

Your assistance will be appriciated.

Sonal
  • 1. Re: Problem in creating log file on Ubuntu(Linux)
    Dude! Guru
    Currently Being Moderated
    It looks like you need to check directory permissions or the actual value of whatever the "strStartupPath" variable represents. If you know the directory, you can change permissions, e.g. chmod u+w,g+w mydir, which will give the owner and group write access.
  • 2. Re: Problem in creating log file on Ubuntu(Linux)
    853982 Newbie
    Currently Being Moderated
    This application needs to run on linux, MAC and Windows. The strStartupPath should be the path where the application is run. This application works fine on a windows or MAC. However we are getting this error while running the same on Ubuntu (we are using the same as a testing OS for Linux). However if I need to provide the solution as you have suggested, I have to first determine the OS and then run this command only for Linux. You will have to help me out with determining the OS as well.

    Thanks for you help.

    Sonal
  • 3. Re: Problem in creating log file on Ubuntu(Linux)
    Dude! Guru
    Currently Being Moderated
    If you want to do this properly then you will have to make it OS specific. Various security systems on each platform will prevent you from freely writing your logfile unless you choose the correct location. The applications folder under MacOSX, for instance, requires Admin privileges. Under MacOSX and Windows, log files are typically stored in the Applications Data resp. /Library/Logs or ~/Library/Logs folder in MacOS X. In Unix and Linux you will typically use /var/log directory.

    I think your question is rather Java than Linux specific, but there is a lot of information available for free on the web.

    Perhaps the following example can help you to determine the OS:
    public static final class OsUtils
    {
       private static String OS = null;
       public static String getOsName()
       {
          if(OS == null) { OS = System.getProperty("os.name");
       }
       public static boolean isWindows()
       {
          return getOsName().startsWith("Windows");
       }
    
       public static boolean isUnix() // and so on
    }
    Edited by: Dude on Apr 16, 2011 12:58 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points