Setting Windows Envirnoment variables within Java code!!

Hello All,
How to setup Windows envirnoment variables within Java code dynamically.
int customer = jList1.getSelectedIndex();
System.out.println(customer);
switch (customer)
case 0:
System.out.println("HOME1");
break;
case 1:
System.out.println("HOME2");
break;
case 2:
System.out.println("HOME3");
break;
case 3:
System.out.println("HOME4");
break;
default:
System.out.println("HOME5");
break;
}

set windows %HOME% variableWhat is it?
A system-wide resource? Something pertinent to the processes of a given user? Or does it apply only to your Java process?

Similar Messages

  • Setting windows environment variables from Java program

    Is there any way to set environment variables from Java program in Windows? Any help is appreciated.
    Here is my situation:
    I need to decrypt an encrypted Oracle user password in a batch file which will be used while running a sql script with sqlplus. I was planning to have bat file which will call a Java program decrypt the password and set it as an env variable in windows which will be available while calling sqlplus.
    thanks

    Runtime.exec has a lot of overloadings. Two of them
    allows you to specify the environment variables.
    exec
    public Process exec(String[] cmdarray,
    String[] envp,
    File dir)
    throws IOExceptionExecutes the specified command and
    arguments in a separate process with the specified
    environment and working directory.
    cmdarray - array containing the command to call and
    its arguments.
    envp - array of strings, each element of which has
    environment variable settings in format name=value.
    dir - the working directory of the subprocess, or null
    if the subprocess should inherit the working directory
    of the current process.
    I had this sample program:
    public class SetVarExample {
    public static void main (String[] args) throws Exception {
         String[] cmd_env= new String[] {"password="+"ABCD","Path=C:\\Sun\\AppServer\\jdk\\bin"};
         String cmd = "cmd /c SET ";
         Runtime.getRuntime().exec(cmd,cmd_env);
    System.out.println( "Finish ...." );
    I tried it in a command prompt. But looks like when the program exits, it's a whole new process and so it does not retain the env variables set in the java program.
    Any suggestions? Am I doing it worng?
    thanks

  • Start weblogic server within java code

    From edoc.bea.com, I got:
    ====
    The following sections describe alternate ways to start an Administration Server:
    Starting an Administration Server from the Windows Start Menu
    Starting an Administration Server When the Host Computer Boots
    Starting an Administration Server With the java weblogic.Server Command
    ====
    Could we start weblogic server within java code?
    (like ServerLoader provided by JBoss?)
    Thanks in advance.
    -Kevin

    The follwoing code:
    //====
    import weblogic.*;
    public class MyTest
    public static void main(String[] args)
              String[] myArgs = new String[6];
              myArgs[0] = "-Dweblogic.home=C:\\bea";
              myArgs[1] = "-Dweblogic.RootDirectory=C:\\bea\\weblogic81\\samples\\domains\\examples";
              myArgs[2] = "-Dweblogic.ConfigFile=C:\\bea\\weblogic81\\samples\\domains\\examples\\config.xml";
              myArgs[3] = "-Dweblogic.Name=examplesServer";
              myArgs[4] = "-Dweblogic.management.username=weblogic";
              myArgs[5] = "-Dweblogic.management.password=weblogic";
              Server.main( myArgs );
    //====
    Seems that did not feed the arguments in:
    C:\myTest>java -cp .;%CLASSPATH% MyTest
    <Sep 22, 2005 6:08:40 PM EDT> <Info> <Security> <BEA-090065> <Getting boot ident
    ity from user.>
    Enter username to boot WebLogic server:

  • Using jsp variable in java code

    Hey guys,
    I need some help hope you guys can help me.
    I've declared a variable "error" in a jsp. how do I access this variable in the java code whice resides in the same jsp page.
    I know I can use java variable in jsp code using
    <%= sVariable %>
    where sVariable was declared in the java code. but how do I access a jsp variable in java code??
    Thanks very much in advance

    Thanks for the reply.
    This is what I want to do.
    I've declared a variable "error" using <c:catch var="error">I want to check the value of this variable and suppose if it contains XYZ (error variable will have a long string so I just want to check if it contains a specific value) I want to do step1 and if the variable of error is not XYZ I want to do step 2
    Problem is I dont know how to check if error contains the XYZ in jsp thats why I thought java code would help here.
    Can you guys suggest a better way to solve my problem.
    Thanks again

  • How to get the Weblogic Server Id from within java code

    I would like to log which server (among a cluster) a certain job is running on. Is there a way to get the server id from within Java code (this code is in a session bean if that is relevant.)
    By server id I mean the "Name" column in the summary of servers on the weblogic console.
    Thanks,
    ken

    Use the two entries close to the bottom of the page: "list WebLogic
    MBeans:listMBeans.jsp
    display MBean attributes and operations:showMBean.jsp"
    Nils
    Anatoly wrote:
    >
    Cameron,
    That page has these items on it:
    which one do you think helps with my issue?
    Misc WebLogic examples
    LongRunningTask
    Execute tasks in parallel using WebLogic Execute Threads
    Weblogic stats (5.1)
    Reload Servlet(s) programmatically (5.1)
    Network classload from WebLogic:using reflection,or the launcher
    Weblogic 5.1 debugging properties
    Seppuku pattern readme
    Using dynamic proxies to intercept EJB invocations (6.1)
    list WebLogic MBeans:listMBeans.jsp
    display MBean attributes and operations:showMBean
    Thanks to Marcelo Caldas for filter by type option and nice UI!
    Using com.sun.jdmk.comm.HtmlAdaptorServer with WebLogic 6.1
    Cool
    EJBGen
    Dimitri
    back
    "Cameron Purdy" <[email protected]> wrote in message news:<3c7a745d$[email protected]>...
    JMX ... see http://dima.dhs.org/misc/ for some info on JMX in Weblogic.
    Peace,
    Cameron Purdy
    Tangosol, Inc.
    Clustering Weblogic? You're either using Coherence, or you should be!
    Download a Tangosol Coherence eval today at http://www.tangosol.com/
    "Anatoly" <[email protected]> wrote in message
    news:[email protected]..
    Does anyone know who to get the managing server URL's port
    from within the EJB code running on Weblogic 6.1?
    The URL port is not default (not 7001), but when creating
    initial context, I am not specifying the URL in properties.
    Due to that, trying to the the PROVIDER_URL property from
    environment does not return anything.
    Appreciate any responses.
    -Anatoly
    ============================
    [email protected]

  • Trouble Setting Windows Environment Variables

    My environment:
    Windows XP Professional
    Intellij 6.0.4
    JDK 1.4.
    I am seeing some very weird behavior when I try to get/set Environment Variables in Windows XP. I use the following code to read the variables:
    Process p = r.exec( "cmd.exe /c set" ); I create an environment varialbe through the Windows control panel, and it does not show up when I execute the above command. However, when I close my IDE and reopen it and run the same code, the variable appears. How is this possible? I am using the Windows XP cmd.exe in my program to read the variables. When I open a DOS window and type the same command, my variable is there.
    This is driving me crazy.
    i want my program to do some work, but then check a system variable to see if it should continue. I want a user to be able to stop processing by setting the environmen variable to true.
    Thanks!
    Nathan

    Runtime.exec has a lot of overloadings. Two of them
    allows you to specify the environment variables.
    exec
    public Process exec(String[] cmdarray,
    String[] envp,
    File dir)
    throws IOExceptionExecutes the specified command and
    arguments in a separate process with the specified
    environment and working directory.
    cmdarray - array containing the command to call and
    its arguments.
    envp - array of strings, each element of which has
    environment variable settings in format name=value.
    dir - the working directory of the subprocess, or null
    if the subprocess should inherit the working directory
    of the current process.
    I had this sample program:
    public class SetVarExample {
    public static void main (String[] args) throws Exception {
         String[] cmd_env= new String[] {"password="+"ABCD","Path=C:\\Sun\\AppServer\\jdk\\bin"};
         String cmd = "cmd /c SET ";
         Runtime.getRuntime().exec(cmd,cmd_env);
    System.out.println( "Finish ...." );
    I tried it in a command prompt. But looks like when the program exits, it's a whole new process and so it does not retain the env variables set in the java program.
    Any suggestions? Am I doing it worng?
    thanks

  • Execute PL/SQL block with named binds from within java code?

    Hi guys,
    Is there any good way to execute my PL/SQL code, for example
    BEGIN         :x := :x+1; END;
    from my Java code? I need nothing complicated, just static code block with named binds.
    I have tried the Oracle exetnded JDBC (setXXXbyName methods):
      public static void main(String[] args){     try {     Class.forName("oracle.jdbc.driver.OracleConnection");     Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","user","password"); String SQL="begin :x:=:x+1; end;"; OracleCallableStatement stmt; stmt=(OracleCallableStatement)conn.prepareCall(SQL); stmt.setIntAtName("x", 5); stmt.registerOutParameter("x", Types.INTEGER); stmt.execute(); System.out.println(stmt.getInt("x"));     } catch (Exception x) { x.printStackTrace();    }   }
    And get the java.sql.SQLException: operation not allowed: Ordinal binding and Named binding cannot be combined!
    Then i've tried SQLJ appoach:
      public static void main(String[] args){     try {     Class.forName("oracle.jdbc.driver.OracleConnection");     Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","user","password");       Oracle.connect(conn);       System.out.println("Connected!");           int x=3;       #sql { BEGIN         :x := :x+1;       END; };           System.out.println("x=" + x);     } catch (Exception x) { x.printStackTrace();    }   }
    And x=3 had retuned... Although 4 expected.
    Then, I've set parameter sqlj.bind-by-identifier=true
    And result is another exception! java.sql.SQLException: Missing IN or OUT parameter at index:: 2
    Can you please mark my mistakes/point to correct solution?
    Thanks in advance,
    Alexey

    Found another solution, this time working at least...
      public void testPLSQL() {
           String dynamicSQL=
                "declare\n" +
                "  v_CursorID  INTEGER;\n" +
                "  v_BlockStr  VARCHAR2(500);\n" +
                "  v_Dummy     INTEGER;\n" +
                "  v_x         String(18);\n" +
                "BEGIN\n" +
                "  v_CursorID := DBMS_SQL.OPEN_CURSOR;\n" +
                "  v_BlockStr :=?;" +
                "  DBMS_SQL.PARSE(v_CursorID, v_BlockStr, DBMS_SQL.V7);\n" +
                "  v_x:=?;"+
                "  DBMS_SQL.BIND_VARIABLE(v_CursorID, ':x', v_x,18);\n" +
                "  v_Dummy := DBMS_SQL.EXECUTE(v_CursorID);\n" +
                "  DBMS_SQL.VARIABLE_VALUE(v_CursorID, ':x', v_x);\n" +
                "  DBMS_SQL.CLOSE_CURSOR(v_CursorID);\n" +
                "  ?:=v_x;"+
                "  COMMIT;\n" +
                "EXCEPTION\n" +
                "  WHEN OTHERS THEN\n" +
                "    DBMS_SQL.CLOSE_CURSOR(v_CursorID);\n" +
                "    RAISE;\n" +
                "END DynamicPLSQL;";
             try {
                   Class.forName("oracle.jdbc.driver.OracleConnection");
                   Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","user", "password");
                   System.out.println("Profit");
         String SQL="begin :x:=:x+1; end;";
         OracleCallableStatement stmt;
         stmt=(OracleCallableStatement)conn.prepareCall(dynamicSQL);
         stmt.setString(1, SQL);
         int x=3;
         stmt.setInt(2, x);
         stmt.registerOutParameter(3,     Types.INTEGER);
         stmt.execute();
         x=stmt.getInt(3);
         System.out.println(x);
         assertEquals(4, x);
             } catch (Exception ex) {
                  ex.printStackTrace();
                  assertTrue(false);
      }Now the only thing I need is to code some kind of preprocessor of SQL block, to prepare the dynamicSQL lair for SQL critter...
    Please please please show me something less complicated! :8}

  • How to use odi variable in java code.

    Hi,
    I am trying to calculate check sum of a file and need to capture that in a odi variable...
    I wrote code like this(selected technology as java bean shell)
    <@
    import java.io.*;
    import java.util.zip.*;
    public class checksum {
    public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(new File("c:/dummy.txt"));
    CheckedInputStream cis = new CheckedInputStream(fis, new CRC32());
    BufferedInputStream in = new BufferedInputStream(cis);
    while (in.read() != -1) {
    #checksum = cis.getChecksum().getValue();
    }@>
    But I am not able to get that value...
    I tried in another way like this
    <@
    import java.io.*;
    import java.util.zip.*;
    public class checksum {
    public static void main(String[] args) throws Exception {
         long result;
    FileInputStream fis = new FileInputStream(new File("c:/dummy.txt"));
    CheckedInputStream cis = new CheckedInputStream(fis, new CRC32());
    BufferedInputStream in = new BufferedInputStream(cis);
    while (in.read() != -1) {
    result= cis.getChecksum().getValue();
    @>
    In odi variable refreshing mode i selected schema as memory_engine and wrote
    select '<@=result@>' from dual;
    even this wont work for me :(
    Anyone pls help me in this...
    Thanks in advance
    Pavan

    Hi Phani,
    Thanks for the reply. I resolved this. The mistake is I wrote java code in a class,in main function. I removed class and main function. Now I am able to capture value into Odi variable...
    I modified code as
    <@
    import java.io.*;
    import java.util.zip.*;
         long result;
    FileInputStream fis = new FileInputStream(new File("C:/dummy.txt"));
    CheckedInputStream cis = new CheckedInputStream(fis, new CRC32());
    BufferedInputStream in = new BufferedInputStream(cis);
    while (in.read() != -1) {
    result=cis.getChecksum().getValue();
    @>
    finally retrieving result value from dual
    select '<@=result@> from dual
    Edited by: 908443 on Jan 16, 2012 10:42 PM

  • Signing Jars from within Java Code

    Hi,
    I would like to sign a jar file from within my Java code. Reason for this is that I wish to update a jar file at runtime, throw away the class loader that loaded the jar, and load the updated code inside a new classloader.
    One problem, however: The jar file has to be signed before loading it. How can I do this from my Java program? Or do I need to have the jarsigner tool available at the location where I resign the jar?
    Thanks in advance,
    Ronald.

    Hi,
    In the meantime I found an answer to my problem. In rt.jar there exists a class sun.security.tools.JarSigner, which can be used for exactly this purpose. Not completely portable, but it'll do the trick.
    Ronald.

  • Is it possible to open a Jar within Java code

    Hey
    I am just wondering if it is at all possible to get java to run a Jar within the code. So the Jar is my update to my program and I have got it downloading from my site using the java code but now i want the code to open it for the user so they don't have to double click in on. Is this possible?
    Thanks

    Sure. The java.util.jar package.
    You might want to take a look at service providers:
    http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Service%20Provider

  • Shell script doesnot execute within java code

    Hi
    I have the following code
    public static void main (String[] args) throws IOException, InterruptedException {
    String command1 = "sh -c /home/test.sh";
    // create a process for the shell
    ProcessBuilder pb = new ProcessBuilder("sh", "-c", command1);
    pb.redirectErrorStream(true); // use this to capture messages sent to stderr
    Process shell = pb.start();
    InputStream shellIn = shell.getInputStream(); // this captures the output from the command
    int shellExitStatus = shell.waitFor(); // wait for the shell to finish and get the return code
    int c;
    while ((c = shellIn.read()) != -1) {System.out.write(c);}
    // close the stream
    try {shellIn.close();} catch (IOException ignoreMe) {}
    }When I run sh -c /home/test.sh from the commandline on my linux box, it works. But when run from within the above java code, it doesnot.Can you please point to what could be missing?
    Many Thanks.

    Nish_B wrote:
    Thanks for the pointer. /home was just an example.
    I modified the code to follow the example.
    String command1 ="/home/test.sh";
    try
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command1);
    InputStream stderr = proc.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    System.out.println("<ERROR>");
    while ( (line = br.readLine()) != null)
    System.out.println(line);
    System.out.println("</ERROR>");
    int exitVal = proc.waitFor();
    System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
    t.printStackTrace();
    You really really do need to read the traps article.
    >
    Is there a way to run as a background process? Use a thread.
    Currently when I run the above, the script runs and holds the input, I tried passing "&" to the command1 string as well as concatenating it to the exec arg but seems like the test.sh script then fails since it takes that as a parameter.Read the traps article.
    So can I run the above script via the java code and let it run as a background process?Read the traps article.

  • How to manipulate Windows System Variables using Java

    Hi There,
    I want to manipulate Windows System variables (e.g. PATH, CLASSPATH) using Java program. Could anyone please let me know how it can be done in Java.
    Regards,
    Rakesh Nagar

    This is not just a Java question. It has never been possible in any Windows program to permanently change the value of an environment variable. You can only change it temporarily, and when the process that changed it ends, so does the change to the environment variable. This has been true since environment variables were added to DOS in about 1983.

  • How to: Transform Activity: Read global/local variable, execute java code

    Could anyone please show me how to do that???

    could you please elaborate your usecase? yes you can read global variable using getVariableData( ) xpath function and pass them as an argument to the processXSLT ( ) xpath function.
    You can read the global or local variables inside java exec using, please refer samples/references/javaexec sample.

  • Running Shell Scripts within Java code!!!

    Hi,
    How do I run a shell script from my Java code???
    Also I need to ensure my next line of Java code is executed only after the shell script has finished executing.
    Since my shell script just cuts out some line for a txt file and then only my Java code parses the cut-out file section and processes certain info.
    Please help!!!
    Thanks!!!

    Runtime.exec and, if you need your Java to stop until the external executable is complete, Process.waitFor.
    And you'll want to read this:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Fetch Unix process ID from within Java code

    Hello,
    if the title doesn't say it all, I need to programmatically get the PID of the JVM process running my application (for monitoring purpose, to ease correlation).
    I found no way in lang classes System nor Runtime or Process. I hoped a system property would be provided, but no luck.
    Any idea?
    I'm using Java 6, on Windows, Solaris, and Linux.
    Thanks in advance,
    J.

    I'm not sure I understand. Until the JVM is started (from the host environment's point of view), I don't know its processID, so I can't put it on, say, the commandLine.
    Once the JVM is started, I can fetch its process ID (at least on Unices, through a ps/grep/awk), but then I have to find a communication medium to feed the JVM with it (e.g. file).
    That makes the application code dependant on the start script, plus other environmental issues (file path, etc.) that may hamper portability. Not to mention, in the File solution, the management of the crashes, when to delete/override the file,etc...
    Some more questions:
    1) is there any simpler solution on Unix/Linux than the ps/grep/awk (I'm far from an Unix guru) to get the PID?
    2) is there any solution on Windows to get the PID from MS-DOS?
    3) is there any other solution that does not involve the OS script language? I take it from my research, and your answer so far, that the answer is nope.
    Brgds,
    J.

Maybe you are looking for

  • How to get ios8 on iphone 4s

    i can not download ios8 on my iphone4s

  • Need help about album artwork/liner!

    I just purchased an album off of Itunes that is sort of random but I needed it for a class. Basically I'm just trying to figure out how to see the inside of the CD, aka the album liner. I tried "Get Album Artwork" and nothing happens. I don't know wh

  • Stopping a Thread (no control on run method)

    Hi, How can we stop a Thread like in the following scenario. If we are in the aMethod() and the stopped variable is set to true by some other thread now how can we return from this run method and stop executing the aMethod(). Any tips are helpful. pu

  • Can't use the facetime function

    I can use the wifi for Internet browsing, and also we have checked our broadband supplier, they stated that there is no problem with the wifi setting, but unfortunately, i can not use the FaceTime function, sometime even I can not access my apple ID,

  • About Actual Cost Process Procedure

    Hai, I want know about , what procudure name oracle using to caluculate the Actual Cost Process item cost...Pls give me all Procedure and related table name. Because one Item Cost is calculate different from prevoius month.I want found all transactio