JDK-8257979 : [macos]:jpackage's working directory is different from javapackager
  • Type: Bug
  • Component: tools
  • Sub-Component: jpackage
  • Affected Version: 15
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: os_x
  • CPU: x86_64
  • Submitted: 2020-12-08
  • Updated: 2020-12-11
  • Resolved: 2020-12-09
Related Reports
Relates :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
MacBookPro16,2/ macOS 10.15.7
jpackage: AdoptOpenJDK 15.0.1
javapackager: Amazon Corretto8.275.01.1 

A DESCRIPTION OF THE PROBLEM :
As reported in JDK-8240571, I had also expected  the working directory to be <application name>/Contents/app from the analogy of the result of javapackager.
I suspect that this behaviour is caused by the modification done at JDK-8226904.
This behaviour may not be a bug, however, I hope that jpackage will work as javapackager.

Andy Herrick added a comment - 2019-06-27 11:37 in JDK-8226904
>this is because main.cpp calls:
>           platform.SetCurrentDirectory(package.GetPackageAppDirectory());
>
> not sure why this was ever put in here. 

https://bugs.openjdk.java.net/browse/JDK-8240571
https://bugs.openjdk.java.net/browse/JDK-8226904


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
**** for jpackage
$ mkdir target
$ /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/javac PwdFrame.java 
$ /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/jar -cf target/pwd.jar PwdFrame.class
$ /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/bin/jpackage --type app-image -i target --main-class PwdFrame --name pwd_jpackage  --main-jar pwd.jar
$ open ./pwd_jpackage.app


**** for javapackager
$ mkdir target result
$ /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home/bin/javac PwdFrame.java
$ echo 'Main-Class: PwdFrame'| cat > MANIFEST.MF
$ /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home/bin/jar -cmvf MANIFEST.MF target/pwd8.jar PwdFrame.class
$ /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home/bin/javapackager -deploy -native image -outdir result -outfile PwdFrame -name pwd_javapackager -appclass PwdFrame -srcdir target -srcfiles pwd8.jar
$ open ./result/bundles/pwd_javapackager.app


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
* jpackage
Current dir:/private/tmp/test/result/bundles/pwd_jpackage.app/Contents/app

ACTUAL -
* jpackage
Current dir:/

* javapackager
Current dir:/private/tmp/test/result/bundles/pwd_javapackager.app/Contents/Java

---------- BEGIN SOURCE ----------
// PwdFrame.java
//
import javax.swing.*;

public class PwdFrame {
    public static void main(String[] args) {
	try{
            String current = new java.io.File( "." ).getCanonicalPath();
	    JFrame f = new JFrame("PwdFrame");
	    JTextArea t = new JTextArea("Current dir:"+current);
	    f.add(t);
	    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    f.setSize(500, 400);
	    f.setVisible(true);
	} catch (Exception e){}
    }
}

---------- END SOURCE ----------

FREQUENCY : always



Comments
Additional Information from submitter: =========================== Thank you for your reply. However, this situation is a problem for existing Java programs that include relative file reference such as File("test.txt"), because the current directory of generated applications by jpackage is always "/" on macOS wherever the applications are located, and jpackage has no option to change the current directory.
11-12-2020

Since the current working directory of an app depend on the method that app is invoked and the OS, there does not seem to be an issue. Closing it as not an issue.
09-12-2020

The jpackage app launcher should not modify the existing current working directory. This is different from javapackager on purpose. javapackager and early versions of jpackage set the current directory in the launcher. This was fixed in JDK-8226904. So the current working dir of an app depends on the method that app is invoked, and the OS. If invoked from a shell, it should be the current dir in that shell, if invoked from finder, it is up to finder, if invoked from shortcut, at least on windows, there is a setting in the shortcut that can determine the directory it is launched in.
09-12-2020