Details
-
Improvement
-
Status: Open
-
Major
-
Resolution: Unresolved
-
None
-
None
-
on all plaforms
-
Important
Description
note that in java/jvm, there is no chdir() function. So groovy also does not have it. Please add chdir() to groovy development kit.
But please note that JRUBY supports chdir(). Jruby do not support it via JNI, but rather they use JNR.
For example, when we launch "irb" of jruby, then see the following session
irb(main):001:0> Dir.pwd()
=> "/home/user/dir1"
irb(main):002:0> Dir.mkdir("dir2")
=> 0
irb(main):004:0> Dir.chdir("dir2")
=> 0
irb(main):005:0> Dir.pwd()
=> "/home/user/dir1/dir2"
So, now users subdir has changed.
I request you to please add chdir() to groovy JDK using JNA or JNR
====================================
this functionality of chdir() using JNA is implemented here as below
/*
reference= https://www.java.net/node/643965
You can use chdir() carefully if you know what you are doing. It's sure that in a multithreaded environment it can be very tricky.
An easy way to implement chdir is using the jna library (https://github.com/twall/jna) like this:
*/
import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform;
import java.io.*;
/**
- Call native low level Posix functions.
*/
public class Posix
{
private static final int MAX_PATH = 1024;
private NativeLibrary cLib = null;
public void chdir(String dirName)Unknown macro: { int error = getCLib().getFunction(getOsDependentFuntionName("chdir")).invokeInt( new Object[] { dirName }); if (error != 0) throw new Error("Could not change working directory to}private NativeLibrary getCLib()
{ if (cLib == null) cLib = NativeLibrary.getInstance(null); // Will load libcxxx.so or msvcrt.dll return cLib; }public String getcwd()
Unknown macro: { String cwd = getCLib().getFunction(getOsDependentFuntionName("getcwd")).invokeString( new Object[] { new String(new byte[MAX_PATH]), MAX_PATH }, false); return cwd; }private String getOsDependentFuntionName(String fname)
{ if (Platform.isWindows()) return "_" + fname; return fname; }}
================================
We need feature of easily implementing native-backed features. Using the FFI API will be the preferred way to bind native OS features, instead of JNI. All the code implemented using FFI(Foreign function interface will be implemented in java, not in C/C++).
Actually, there are two competing mechanism to support this native OS functionality access in java code:
Method1) JNA(java native access) = https://github.com/twall/jna
Method2) JNR(java native runtime) = used by jruby https://github.com/jnr
this JNR will be part be part of of Java in future http://openjdk.java.net/jeps/191
But for now, it is bundled with jruby itself . open jruby-1.7.20.1/lib/jruby.jar in any unzip program and then you will see that it already uses jnr containined in 'jnr" direcotry inside that jruby.jar file
Both methods work fine
========================================================
Note that as jruby is using jnr and may be in far future jnr may be part of jvm itself.
So, it may be advised to use jnr, but still i have given this above illustrated code using JNA, which functions identically with the same jna-..jar bundled with on MS-Windows,linux,BSD etc
IF groovy implements this feature, then groovy will have to include .jar file of jna or jnr , whatever route it implements the code.
as groovy aspires to be a realworld scripting language in competition with jruby and jython, so this inbuilt chdir() function is very much needed, urgent and useful.