Executing batch file as administrator changes working directory

hey guys,
I already posted this question on the msdn-forums and they sent me here. I hope, you can help me.
My situation: I have a java program which I compile with exe4j. exe4j adds some nice features so I can install the program as a service with the command "myexe.exe /install"
I put that command in a batch file (more user friendly).
The problem: Only administrators can install services (in Win 7). But if I run the batch-file as administrator, the working directory changes to the system32-directory and I get an "command not found"-error.
The question: How can I solve this? The only solution I know at the moment is setting a environment variable at installation time that points to the install-location. Is there another way?
Thx for your help in advance

With runas you in fact logon to another seesion. So cmd is started with his own starting directory (being where it is located, system32).
I would recommend never to rely on the working dir for a batch file or script, but to always include the full path to the needed command/executables. If needed you can use environment variables to get the pathes of user-customised folders (for example the %usersprofile% variable).
Using .lnk files (normal shortcuts) you can specify the working dir to start in if you would really need that functionality.
MCSA/MCTS/MCP

Similar Messages

  • Executing batch file from Java stored procedure hang

    Dears,
    I'm using the following code to execute batch file from Java Stored procedure, which is working fine from Java IDE JDeveloper 10.1.3.4.
    public static String runFile(String drive)
    String result = "";
    String content = "echo off\n" + "vol " + drive + ": | find /i \"Serial Number is\"";
    try {
    File directory = new File(drive + ":");
    File file = File.createTempFile("bb1", ".bat", directory);
    file.deleteOnExit();
    FileWriter fw = new java.io.FileWriter(file);
    fw.write(content);
    fw.close();
    // The next line is the command causing the problem
    Process p = Runtime.getRuntime().exec("cmd.exe /c " + file.getPath());
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = input.readLine()) != null)
    result += line;
    input.close();
    file.delete();
    result = result.substring( result.lastIndexOf( ' ' )).trim();
    } catch (Exception e) {
    e.printStackTrace();
    result = e.getClass().getName() + " : " + e.getMessage();
    return result;
    The above code is used in getting the volume of a drive on windows, something like "80EC-C230"
    I gave the SYSTEM schema the required privilege to execute the code.
    EXEC DBMS_JAVA.grant_permission('SYSTEM', 'java.io.FilePermission', '<<ALL FILES>>', 'read ,write, execute, delete');
    EXEC DBMS_JAVA.grant_permission('SYSTEM', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
    EXEC DBMS_JAVA.grant_permission('SYSTEM', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
    GRANT JAVAUSERPRIV TO SYSTEM;
    I have used the following to load the class in Oracle 9ir2 DB:
    loadjava -u [system/******@orcl|mailto:system/******@orcl] -v -resolve C:\Server\src\net\dev\Util.java
    CREATE FUNCTION A1(drive IN VARCHAR2) RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'net.dev.Util.a1(java.lang.String) return java.lang.String';
    variable serial1 varchar2(1000);
    call A1( 'C' ) into :serial1;
    The problem that it hangs when I execute the call to the function (I have indicated the line causing the problem in a comment in the code).
    I have seen similar problems on other forums, but no solution posted
    [http://oracle.ittoolbox.com/groups/technical-functional/oracle-jdeveloper-l/run-an-exe-file-using-oracle-database-trigger-1567662]
    I have posted this in JDeveloper forum ([t-853821]) but suggested to post for forum in DB.
    Can anyne help?

    Dear Peter,
    You are totally right, I got this as mistake copy paste. I'm just having a Java utility for running external files outside Oracle DB, this is the method runFile()
    I'm passing it the content of script and names of file to be created on the fly and executed then deleted, sorry for the mistake in creating caller function.
    The main point, how I claim that the line in code where creating external process is the problem. I have tried the code with commenting this line and it was working ok, I made this to make sure of the permission required that I need to give to the schema passing security permission problems.
    The function script is running perfect if I'm executing vbs script outside Oracle using something like "cscript //NoLogo aaa1.vbs", but when I use the command line the call just never returns to me "cmd.exe /c bb1.bat".
    where content of bb1.bat as follows:
    echo off
    vol C: | find /i "Serial Number is"
    The above batch file just get the serial number of hard drive assigned when windows formatted HD.
    Same code runs outside Oracle just fine, but inside Oracle doesn't return if I exectued the following:
    variable serial1 varchar2(1000);
    call A1( 'C' ) into :serial1;
    Never returns
    Thanks for tracing teh issue to that details ;) hope you coul help.

  • How to list the top 10 largest files in the current working directory ?

    How can I list the top 10 largest files in the current working directory in Solaris ?

    execute below....to get the large files in order.. change the <FS>
    find /<FS> -type f -size +1024 -ls | awk '{print $11, $7}' | sort -rn +1 | awk '{print $1, $2/1024/1024 "MB"}' | /bin/more

  • Execute batch file having gmake command

    Hi,  I am executing batch file having gmake command in it, when i run batch file directly it works correctly,
    but when i try to execute it from LabVIEW using system Exec.vi  
    i got following error,  
    process_easy: DuplicateHandle(In) failed (e=6)
    process_easy() failed to launch process (e=6)  
    Thanks in advance

    What OS?
    How are you calling it in LV?
    What is in the batch file?
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • How to execute batch file from JSP

    hi frens !
    i wanna know how to execute batch file from my JSP.i am using exec() method to get call the batch file. but its not working ....plz help
    here mine code:-
    File F = new File("C:/var.bat");
         try{
          if (F.exists())
            Runtime rt = Runtime.getRuntime();
            String url=F.getAbsolutePath();
             Process proc = rt.exec(url);
            proc.waitFor();
            proc.destroy();
            catch(Exception IOEx){
           System.out.println(IOEx);
      }Thanks and Regards
    Allwyn

    You might improve your chances of getting help if you do two things:
    1) Explain what "not working" means.
    2) ChangeSystem.out.println(IOEx); to IOEx.printStackTrace(); (in the eventhat is in fact related to "not working").

  • Executing Batch files from Runtime.getRuntime().exec

    Can we execute batch files from Runtime.getRuntime().exec() method.
    Regards,
    Nalini

    hi,
    import java.io.IOException;
    class BatchFileTest{
         public static void main(String args[]){
              try{
              Runtime r = Runtime.getRuntime();
              Process p = r.exec("startcmd.bat");
              catch(IOException en){
                   en.printStackTrace();
    -Regards
    Manikantan

  • Running executable in batch file as administrator when UAC is high

    Hi Team,
    I have a requirement of running executable in batch file by passing the current windows login username as parameters. Below is the content of my batch file:
    cmd /c start setup.exe http://testsite:7001/mobile  %username%  %username%
    But, when the UAC is set high in windows 7 machine, this executable is not running properly, and giving me warnings. And, if I am running the same start command after opening command prompt as administrator, then executable is running smoothly.
    So, is there any way I can run the executable as administrator by giving some options in batch file, when UAC is high?
    I tried runas option, but its prompting for admin password, which we can't provide as users will not know.
    Do we have some option in batch file as 'Run As Administrator' right click option, where it doesn't prompt for password but run the file with admin privilege?
    Thanks,
    Tina Soni

    Hi,
    When using Runas command, you can add a switch option /savecred
    /savecred will make it ask for admin password on the first run only, after that it should be saved
    Yolanda Zhu
    TechNet Community Support

  • When triggering a UBE filter to run an external batch file, it doesn't work, although the rule is triggered.

    UBE filters are not running a batch file as
    an external program. The batch file is located in the postoffice spool
    directory and has the correct permissions.
    <P>
    Change the RUN command to REJECT to check that it was actually triggered.
    <P>
    <B>Background:</B>
    <P>
    Example:
    <P>
    filter.cfg is as follows:
    <P>
    :Test Channel-To "ecxpert@host\.domain\.com" RUN "test.bat"<BR>
    :sendback Channel-To "ecxpert@host\.domain\.com" COPY "pvtest1@host\.domain\.com"
    <P>
    filter.opt is as follows:
    <P>
    # To parse header fields as well as envelope fields remove the comment (#) from the following<BR>
    # line. See Help for more information<BR>
    parseheader:1
    <P>
    test.bat resides in C:\Netscape\Suitespot\MailServer\Spool and is as follows:
    <P>
    date /t >> test.txt
    <P>
    This would be 100% correct for any Unix system, with the addition of a top line
    to the script determining what shell to use. However, with NT, shell type
    definition is not part of a batch file.
    Correcting the UBE filter in filter.cfg to read as follows cures the problem:
    <P>
    :Test Channel-To "ecxpert@host\.domain\.com" RUN "cmd.exe /c test.bat"
    <BR>
    :sendback Channel-To "ecxpert@host\.domain\.com" COPY "pvtest1@host\.domain\.com"
    <P>
    The following also works:
    <P>
    :Test Channel-To "ecxpert@host\.domain\.com" RUN "cmd.exe /c date /t test.txt"<BR>
    :sendback Channel-To "ecxpert@host\.domain\.com" COPY "pvtest1@host\.domain\.com"
    <P>
    The reasoning behind this is that batch files are scripts for CMD.EXE, the
    NT Shell, and therefore cannot be executed directly. CMD.EXE has the /C
    switch to run a following command, or batch file, and exit on completion.
    <P>
    Another solution would be to write an executable program in BASIC or any
    other chosen language to perform the same functions. However, do compile it to an
    executable before trying to use it since, for example, a *.BAS file will
    run in Windows but it means nothing to Messaging Server.

    Look at the documentation of Runtime.exec()
    There are versions of that method that allow you to specify the name of an executable (the name of your batch file) and also an array of strings with environment parameters. I've never used this myself, but it should work like this (lookup the documentation!):
    Runtime.getRuntime().exec("bla.bat", new String[] { "ROOTDIR=" + rootdir });

  • External task -  run batch file as administrator

    Hi,
    My name is Sid. I am working on developing an EDQ project to parse a collection of log files.
    I have run into a roadblock on the first step!
    My EDQ server is on a Windows Server 2008 platform.
    I am calling a batch file that copies these log files between two locations.
    The batch file copies the files fine when i run it as administrator from the commandarea in  EDQ server, but it does not work when called from the External Tasks in EDQ.
    I tried using the runas user:/Administrator in the batch file but that does not work either.
    Would greatly appreciate any pointers in this regard.
    Thank You,
    Sid

    With runas you in fact logon to another seesion. So cmd is started with his own starting directory (being where it is located, system32).
    I would recommend never to rely on the working dir for a batch file or script, but to always include the full path to the needed command/executables. If needed you can use environment variables to get the pathes of user-customised folders (for example the %usersprofile% variable).
    Using .lnk files (normal shortcuts) you can specify the working dir to start in if you would really need that functionality.
    MCSA/MCTS/MCP

  • Problem Executing Batch File

    I wrote out a java class that I tried using cfobject to call. It didn't work. So I wrote a batch file to call the java class.  The batch file works, but NOt when I have coldfusion call it. Nothing happens, no error... nothing.  I thought it might be a permissions issue, but I tried changing permissions for coldfusion although I may have doneit wrong since I'm not too familiar with Vista.  Can anyone offer advice on what I can do.  Its a simple java class and I can't get it to run.  no errors in the log.  There isn't much for me to go on.  All I know is that the class and batch file work fine if Coldfusion doesn't call them.

    Amosl wrote:
    To limit the problems with the class itself and cfobject, I've created a batch file to run the class.  The batch file runs the class fine, so long as I execute the batch via command line.  But coldfusion doesn't execute the batch with cfexecute.  the same exact problem with absolutely no error messages.
    Unfortunately, cfexecute is not the easiest tag to debug. In my experience sometimes it confuses more than clarifies.  But trying it is not a bad idea from a troubleshooting point of view.
    However,  if I create a java class in the same folder that doesn't write to the system and only returns variables, it works fine.  This indicates the class path is set correctly and that cfobject does work.
    Yes, I would agree.
    It does sound like it might be a permissions issue. But I have little experience with Vista, so there may well be another cause.  Since it sounds like you are just testing a very simple java class, can you post the code for the class itself as well as the exact cfexecute code you are using? I could test it on another operating system. Just to see if there is some obvious problem.
    Amosl wrote:
    From the 100's of pages I've read online with people with similar problems, this seems to be a permissions issue.  however, I've set Coldfusion to run as me, the administrator and only person who uses this computer.  It is no longer running as local system.
    However, this didn't fix anything.  My only guess is that perhaps I've no set it to run as me correctly.  But I have no way of testing this.  Windows logs show no error messages either.  Is there any way to have coldfusion show me what user its trying to execute the batch as.
    I believe the user account can be obtained from the java system properties.  Try:
    <cfset sys = createObject("java", "java.lang.System")>
    <cfoutput>
       <b>user.name</b> = #sys.getProperty("user.name", "not found")#<hr>
       <b>coldfusion.classPath</b> = #sys.getProperty("coldfusion.classPath")#<br>
    </cfoutput>

  • Executing batch file using SM49 Tcode

    Hi,
    Theirs a batch file which is used to encrypt a text file ie from '.txt' to '.enc'.
    Seperate Encryption software and Java Pakage is used.
    When in SM49 the operating system command is executed,its throwing error
    File Not Found Exception.
    ie call failed
    Java.io.filenotfoundexception :  ..\logs\star_pkcs7-client-events.log(The system cannot find the path specified)
    OS is WinNt,File is encrypting at OS level seperately.
    All authorisation issue has been taken care of.
    If any questions or need information then plz ask
    Help me on this...its urgent

    hi,
    well try the following
           String cmdLine[] = {"cmd.exe","/c","copy.bat"};       Runtime.getRuntime().exec(cmdLine);it works fine in Win NT, the process terminates normally.
    cheerz
    ynkrish

  • Executing batch files from websevice using ODI OS Command

    Hi,
    Is it possible to execute the batch files using odi os command from webservices.
    We have developed a webservice, which passess some parameters to one batch file and after that we executing the same batch file in package using odi os command. In opeartor is showing as running, it never ends.
    But if we run the same package from designer, it is executing successfully. Also the same web service is working fine for executing non batch scenarios.
    what could be the problem??
    We got one possible reason for failure...
    For executing the batch from web services, we need read and executable permissions on the drive where batch sits.
    We are logging into ODI using SUPERVISOR. How to give the permissions to SUPERVISOR as we are having windows authentication.
    Edited by: Naveen Suram on Dec 17, 2009 8:07 PM
    As a work around, can we use ODIOSCommand instead of OS Command. But it is asking for starting event? Whether both the commands does the same functionality??
    Edited by: Naveen Suram on Dec 17, 2009 9:08 PM

    From the FAQ:
    2. How do you launch an external program on a Microsoft Windows platform from a program developed on the Java [tm] programming language?
    The following will launch notepad in Microsoft Windows NT:
    Runtime.getRuntime().exec("cmd /c notepad.exe");
    To launch a program in Microsoft Windows 95/98 use:
    Runtime.getRuntime().exec("c:\\windows\\notepad.exe");
    The Runtime class allows interaction between a program and its environment. The first string command instructs the command line interpretor, cmd to open up the calculator application.
    The exec() methods do not use a shell; any arguments must have the full pathname to the shell as well as the command itself.
    For example, to run a shell on the UNIX� platform, type:
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("/usr/bin/sh -c date");
    To run a batch file under Microsoft Windows 95/98:
    Process p = rt.exec("command.com /c c:\\mydir\\myfile.bat");
    To run a batch file under Microsoft Windows NT:
    Process p = rt.exec("cmd /c c:\\mydir\\myfile.bat");
    where 'cmd' and 'command.com' are the command-line interpreters for Microsoft Windows machines.
    The Runtime.exec() methods might not work effectively for some processes on certain platforms. Special care should be taken with native windowing, daemon, WIN16/DOS process or some shell scripts.
    regards,
    jarshe

  • Two questions: Executing batch files and executing a JAR on boot-up.

    I'm working with Windows XP and and currently I'm having a friend test this on Vista as well. I can't seem to find how to execute a batch file using Runtime.exec(). So far I've been passing the absolute pathname of the batch file as the argument to exec(), but it doens't seem to run, so I'm assuming there's another way.
    Second question, how do I get a JAR file to start running whenever the user turns on the PC?
    Thanks.

    I'm working with Windows XP and and currently I'm
    having a friend test this on Vista as well. I can't
    seem to find how to execute a batch file using
    Runtime.exec(). So far I've been passing the
    absolute pathname of the batch file as the argument
    to exec(), but it doens't seem to run, so I'm
    assuming there's another way.Are you using just Runtime.exec() or have you tried Runtime.getRuntime().exec()? If not, try that and see what happens.
    Second question, how do I get a JAR file to start
    running whenever the user turns on the PC?As far as I know, you can't do that programatically. You have to edit settings on your computer but I am not entirely sure.
    Thanks.

  • Execute batch file

    Hello,
    I have a simple and short batch file I am trying to execute from SQL server. The SQL database I am connected to is my local instance. The script I am using is as below:
    EXECUTE [master].[dbo].[xp_cmdshell] 'C:\Users\Jdoe\Desktop\Test.bat'
    The batch file is located at the path listed above and contains the below:
    SchTasks /query /fo csv > I:\Schedtask.txt /v /s reports.xyz.org
    The above command line code connects to a remote machine and gets the list of all scheduled tasks. Please note that executing the batch file by double-clicking on the icon works without issues. When I try however to run the SQL listed above, I get the below:
    output
    NULL
    C:\Windows\system32>SchTasks /query /fo csv /v /s reports.xyz.org 1>I:\Schedtask.txt
    The system cannot find the path specified.
    NULL
    What am I doing wrong. I am expecting the SQL to execute de batch file which should create a output file at the specified location but it doesn't.
    Thanks for helping

    Try to use UNC path (\\server\share\file_path). Also make sure that the doamain account used as task owner has the proper rights on the source and destination
    Javier Villegas |
    @javier_vill | http://sql-javier-villegas.blogspot.com/
    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you

  • Execute Batch Files, DOS Commands etc. from my Java Application

    Can someone show me how it works?
    I dont need any return values, just execute a Batch File or a Command
    Thx, Guardian

    Hi Luke,
    Just wanna add 2 things to what you've written. (your sample code)
    Point 1pro.waitFor(); should be called before attempting the read anything from the process' InputStream if you don't want to lose anything your process might print out. When Runtime.getRuntime.exec("your_command") is executed, the resulting process is just another runaway thread unless you tell you calling thread to wait for this process to end before proceeding further. This is where waitFor() comes in.
    Point 2Runtime.getRuntime().exec("C:\\command.com /C mybat.bat"); works but it is pretty platform dependent. If you need to write platform independent stuff, you could just drop the command.com stuff and go right to the batch file or the command you need to execute. I've managed to just survive with Runtime.getRuntime().exec("mybat.bat");Regards,
    Noel

Maybe you are looking for

  • What kind of external hard drive should I buy?

    I have a MacBook pro and an older iMac desktop at home, and several different PC's at school and work (dells for the most part). The files I need to move around are very diverse, from .psd and .ai to google sketchup to CAD to mp3s and PDFs. I need to

  • Adobe Premiere Pro CS6 has stopped working

    I have brand new Samsung 5 series laptop Windows 8 Intel core i7 Radeon Graphics AMD AMD HD8750M gDD3 2Go_Dual 8 Go (DDR3 1600MHz/4Go*2) 1T I open Adobe Premiere Pro CS6. I import a .mov file. I move the .mov file into the next box and a message pops

  • Report on Top-10 Ship-to-parties

    Hi, I want to create a report on Top-10 Ship-to-Parties based on the Open Orders. So 0SD_C03 Infocube will workout or shall i use any other cube. And of Ship-to-Party and Open orders which Info object shall i use. Please explain how to develop th rep

  • Crystal report could not show dynamic picture

    I develop crystal report which show dynamically picture in my PC but when I send it to server which has not be installed program just install asp.net framework 2.0 , it could not show dynamic picture. How should I do, please advise.

  • Suitcase Fusion causing FCP bug

    Just thought id post this, Since installing Suitcase Fusion when i export a qt from fcp, where the drop down box asks you what type of markers, if any, you would like to include, rather than having the choices none,chapters only etc etc i just get a