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>