Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Heh heres a tricky java one.

  • 18-10-2004 5:16pm
    #1
    Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭


    Trying to find a solution to this and kind of banging my head.

    How do you change the directory you are in, within Java?

    eg.

    c:\folder1\> java myProg
    Program finished.
    c:\folder2\>_


Comments

  • Closed Accounts Posts: 79 ✭✭zt


    Yes. You are right. It is tricky. What platform?

    My memory suggests the native call is chdir() on UNIX.

    I wonder will Java mess this up though. The security model might try to stop you from modifying the environment.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    By program finished, are you still within the main method, or has the complete class finished executing? Because as far as I know without using a JNI call you won't be able to change the directory that a terminal is pointing to.


  • Registered Users, Registered Users 2 Posts: 834 ✭✭✭fragile


    If my memory serves me correctly you can call setCwd() and getCwd(), but then again i havn't used Java in about 3 years so YMMV


  • Closed Accounts Posts: 79 ✭✭zt


    By program finished, are you still within the main method, or has the complete class finished executing? Because as far as I know without using a JNI call you won't be able to change the directory that a terminal is pointing to.
    I suspect he would like a java version of cd

    [root@w8 tmp] javacd /home/myHome
    [root@w8 myHome] pwd
    [root@w8 myHome] /home/myHome


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Here is what I am looking for.

    A long long time ago I wrote a nice little utility that did the following...

    Imagine two directories.
    c:\project\src\com\my\package\is\here
    c:\project\src\com\your\package\is\here

    Now to move between the first one and second one you would have to type one of the following.

    cd ..\..\..\your
    cd c:\project\src\com\your\package\is\here

    (there are other options now in XP, eg you can use wildcards but lets ignore that).

    So my little app basically you typed..

    myApp your

    it would then work backwards through the path until it found a matching one with the word that was replaced.

    eg.
    c:\project\src\com\my\package\is\your - No Match
    c:\project\src\com\my\package\your\here - No Match
    c:\project\src\com\my\your\is\here - No Match
    c:\project\src\com\your\package\is\here - Match Change to this dir.

    To write it is very simple however I don't have any compilers to create EXE's, Batch files won't cut it and Java doesn't allow change directory. :/

    So if there is a free compiler (to create an EXE) out there for a language I'll settle for that.


  • Advertisement
  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    zt wrote:
    I suspect he would like a java version of cd

    [root@w8 tmp] javacd /home/myHome
    [root@w8 myHome] pwd
    [root@w8 myHome] /home/myHome
    I'd figured what he was looking for, I was just saying that its not possible using a 100% Pure Java Solution.


  • Closed Accounts Posts: 79 ✭✭zt


    I've a solution for Unix that uses part script/part java. I also propose a solution for Windows that is a bit tacky, but maybe somebody could think of a clean windows equivalent?

    Basically you get the Java piece to generate output that is piped or executed by a script.

    This worked on Linux. Make sure you use the correct quotes ``.

    Here is the code and output:

    [root@ether zt]# pwd
    /home/zt
    [root@ether zt]# `java Test`
    [root@ether tmp]# pwd
    /tmp
    [root@s3 tmp]#

    Here is the very simple mainline from Test.java:

    public static void main(String args[]) {
    System.out.print("cd /tmp");
    }

    The only way (I can think of) on Windows is to get the Java code to dynamically create a bat file and then execute this using call. I've never really done any dos scripting and somebody may suggest a better solution.

    REM mycd.bat
    del %temp%\cd.bat (make sure the previous file is removed)

    java FindPath %1 > %TEMP%\cd.bat

    call %TEMP%\cd

    etc ...

    FindPath.java would be along the lines of:

    static int main(String args[]) {
    String path = workoutPath(args[0]);

    System.out.print("CD " + path);
    }


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    https://lists.xcf.berkeley.edu/lists/advanced-java/2003-February/019324.html

    An explanation of why it has to be done the above way is here


  • Closed Accounts Posts: 92 ✭✭tempest


    It can't be done in java (without writing native code) which means you need a native compiler eitherways. Reason being, as much as any, is that the virtual machine is the actual application so you need to change the cwd of the virtual machine, not the program.

    http://www.borland.com/products/downloads/download_cbuilder.html is a good C/C++ compiler and it's free. (For personal use at least, which this sounds like.)

    Other than that you could try http://www.mingw.org/.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Yea. VBScript doesn't do it either, or rather it does but then reverts back when the program is finished.

    Probably go with the batch file creation script. That way I can code it to use pushd instead of cd.


  • Advertisement
  • Closed Accounts Posts: 47 PhilH


    Getting to this a bit late, but I have a little suggestion to add to zt's suggestion. Rather than have a Java program that outputs the text of the command you want to run (which would be OS dependant), you get the Java program to output the absolute path to the directory you want to switch to (which Java should be able to do appropriately to your OS). Then you use the backquotes in Unix to accept that as the directory you want to move to...
    cd `java Test blah`
    

    Now that can be wrapped in a little script, just for tidiness.

    On DOS (well, who are we kidding here?) you need the batch file. You want to get the output of your Java program into an environment variable, and CD to that. This is messy in batch files, but looks like this...
    for /F %%i IN ('java Test blah') DO set DEST_DIR=%%i
    CD %DEST_DIR%
    

    I had to do this a little while ago, not for changing directory, but so the output from one java program (which calculated and formatted dates) could be used as command line arguments to another.

    PHiL


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Got it working, however in relation to your batch file.

    I had the java app return ?c:\mydirectory\etc?

    Then used..

    for /F "delim=?" %%i IN ('java Test blah') DO set DEST_DIR=%%i

    This is to stop paths with spaces in them from being chopped off.


  • Closed Accounts Posts: 47 PhilH


    Ah ha... spaces, hadn't thought of that.

    Glad to see you got what you wanted done.


Advertisement