0

Yeah, this is totally strange… Not sure why this is happening, but I have a simple Java Swing program that, when the user clicks the button, it creates a new directory in the working directory.

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
  * Created by jasper on 11/28/15.
  */
 public class JarBundlerTest extends JFrame{

    public JarBundlerTest(){
        setTitle("JarBundler Test");
        setLayout(new BorderLayout());

        JButton button = new JButton("Create File");
        button.addActionListener((ActionListener) -> createFile());
        add(button, BorderLayout.CENTER);

        pack();
        setMinimumSize(getSize());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public void createFile(){
        File f = new File("./newfile");
        if(f.mkdirs()) {
            JOptionPane.showMessageDialog(null, "Created directory " + f.getPath());
        }else{
            JOptionPane.showMessageDialog(null, "Could not create directory.");
        }
    }

    public static void main(String[] args){
        new JarBundlerTest();
    }

}

On my Mac, it works when bundled into a Jar file and moved to the Desktop. But when I bundle it into an app file using appbundler, the dialog shows up that says "Could not create directory". I'm using <option value="-Duser.dir=$APP_ROOT"/>, which puts the working directory in the application bundle. It works with this line omitted, but I want the working directory to be in the app bundle.

But here's the weird thing: when I call f.getAbsolutePath(), it returns the correct file path. I know the directories don't exist and I have permission to do so. Why is the directory not being created? How do I fix this?

Here's my build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="tempBuildFile" default="bundle-app" basedir="/Users/jasper/Desktop">
    <description>Temporary build config for ant script</description>
    <taskdef name="bundleapp"
        classname="com.oracle.appbundler.AppBundlerTask"
        classpath="/Users/jasper/ant/lib/appbundler-1.0.jar" />
    <target name="bundle-app">
        <bundleapp outputdirectory="/Users/jasper/Desktop"
            name="JarBundlerTest"
            displayname="JarBundlerTest"
            identifier="JarBundlerTest"
            shortversion="1.1"
            icon="/Users/jasper/Desktop/developer/java/lib/GenericApp.icns"
            mainclassname="JarBundlerTest">
            <classpath  file="/Users/jasper/Desktop/JarBundlerTest.jar"/>
            <option value="-Dapple.laf.useScreenMenuBar=true"/>
            <option value="-Duser.dir=$APP_ROOT"/> 
        </bundleapp>
    </target>
</project>
DrOverbuild
  • 1,245
  • 10
  • 11
  • 1
    Instead of `File f = new File("./newfile");`, try `File f = new File(System.getProperty("user.dir") + "/newfile")`. The `user.dir` property is the current working directory. – Lucas Baizer Nov 28 '15 at 20:13
  • I think you'll find there are a numbers of restrictions that enforced by the OS with regards to the app directory. Under Mac OS it's better to use {user.dir}/Library/Application Support/{your application} to store application support files – MadProgrammer Nov 28 '15 at 20:21
  • @Lucas Baizer Thanks! Did think to try that for some reason. – DrOverbuild Nov 28 '15 at 20:54
  • @MadProgrammer I'm actually doing this for the users of my program called JarBundler, who want the working directory to be in the same directory as their app. I've told them to use ~/Library/Application Support/, but they don't listen… – DrOverbuild Nov 28 '15 at 20:56
  • Maybe you should tell them they need to talk to Apple, because it's Apple restrictions, not Java/JarBundler – MadProgrammer Nov 28 '15 at 21:14

0 Answers0