Running SET command using Java - java

I am executing an application using Java application (Runtime.get...) but now before running the application I have to set temp path.
set tmpdir=%temp%
Is it anyway I can execute above command using Java?

Running set in a separate Runtime.exec() invocation wouldn't help at all: it only ever affects the process that it runs in and each exec() call produces its own process.
What you need to do instead is provide the environment variable to your Runtime.exec() call using this two or three argument variant.
Better yet, scrap Runtime.exec() and use ProcessBuilder instead. With this you can simply use environment().put("tmpdir", "somevalue") to set the environment variable you want (you can even get the value of %temp% from that Map).

Could you execute a .cmd/.bat file rather than the app directly?

Related

How to read "OS variables" from Java (not System.properties)

I just read the excellent 12 Factor App document and it really registered with me. In Chapter 3 Config, the author stresses that:
The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.
I'm wondering what this implies for Java projects.
Typically, when it comes to my Java apps, I think of "env vars" as things that I can read from System.getProperty("blah"). Is this what the author is talking about? I don't think this is, because the author explicitly states "and unlike...or other config mechanisms such as Java System Properties...".
If not, what would be an example of an OS-agnostic "env var" that would satisfy this document's definition of "env var"? How would I read it from inside Java code?
Either way, some process has to first set each env var on the OS, so that the var is set & available by the time the app runs. What processes/methods could do this type of pre-run setup?
Use System.getenv() instead of System.getProperty(). System.getenv() returns value of specified environment variable defined in your OS.
Other, and probably preferable way is to pass selected OS environment variable to your JVM using -D command switch and then retrieve them using System.getProperty(). This way is more cross-platform: you may use the same java code even if specific platform does not support specific variable.
Updating the OS variables is absolutely other task. First, I do not think that you really want to do this. If you still want try to ask another question: probably other solution exists.
There is no cross platform API that does this in JDK and I do not know 3rd party library that dies this too. I personally planned to implement one but did not have time for this so far.
Windows stores variables in registry, so you can add variable to registry. But this does not affect current instance of shell, so your own java process will not see variables that it updated itself. You can however create process and run SET myvar=myvalue. If you want to write to registry you can use any of available libraries. Occasionally I implemented such library too. Its advantage is that it does not run any native code and is very compact. Take a look here.
On Unix you can run myvar=myvalue and if you want other processes to see this variable you have to run export myvar. But it still does not make this variable persisted. If you want this variable to survive OS restart you have to add it to one of the initial scripts (e.g. .bashrc). But this is completely depends on your system, configuration, permissions, shell etc.
Yes, the author is talking about environment variables. You can read environment variables from java code using the following snippet:
System.getenv(env);
See: http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getenv-java.lang.String-
If you want OS agnostic way, you can use JVM arguments and read using
System.getProperties();
For example, if you want a variable called var1 to be passed, use java -Dvar1=true -jar myJar.jar

Calling shell script from java program and passing arguments to a shell script from java for loop

Does anyone know if it is possible to call a shell script from within Java program and also pass an argument to that shell script from the for loop of that java class? In my shell script I am setting MySQL system variables to different values to see if those values affect the performance of the database application. I could have set those values through JDBC, but as I am working with MySQL, it is not possible to restart the database from JDBC, after each query execution.
Yes it is possible. For something like this you would probably be better off just using a batch file or something to do it though.
If you really do need to use java try:
How to run Unix shell script from Java code?
Runtime.exec() is what you are looking for.
Runtime.getRuntime().exec("theScript.sh param1 param2);
..modulo exception handling.
You may want to look into Apache Commons

how can i set -ORBInitialRef without using cmd?

i cleaned and built my program in net beans. i want to use jar file but i dont know how can i set -ORBInitialRef ?
java RunServer -ORBInitRef NameService=corbaloc::localhost:777/NameService
Is it some kind of a java VM argument (or System property) specified like
java RunServer -DORBInitRef <value>
because simply passing the values to the java command is passing arguments to the RunServer program.
It doesn't really matter whether it's a command line argument, or a JVM system property that's set, as long as it gets passed into the ORB.init() function. The CORBA Orb init() function is smart enough to parse the arguments and pass the arguments that begin with -ORB along during ORB creation.
Take a look at some java ORB initialisation examples here: http://www.novell.com/documentation/extend52/Docs/help/MP/orb/tutorial/orbInit.html

Create an environment variable programmatically

I have an external program that I'm running.
for some reason, the code owner didn't give me the code or and good documentation, I know how to run this code but it was written originaly to be executed from command line and not from JAVA.
the effect on me is that this application uses an ENV variable and relay on its value (a path on the computer for the output).
I want to change that value, how can it be done without running it from a batch file?
I assume you are executing this program using one of the Runtime.exec() methods in Java code to create a Process.
Note that some of those methods allows you to pass environment variables to the process you are creating, for example exec(String[] cmd, String[] envp).
Alternatively, the Map returned by ProcessBuilder.environment() can be manipulated for the same effect.
how can it be done without running it from a batch file
Just set global environment variable. All new processes will see it (excluding those inheriting environment from old parent process).
See also How do I set environment variables from Java?.
This answers the question's title. Which doesn't match the question's body, btw. ;)
See this post. It usually helps to first start a search here before posting a question. If you already tried that solution, it really helps the Helpers to let them know that you tried it and what went wrong.
In your command prompt first set the required variable
set FILELOCATION=<PATH TO FILE>
java MyProgram
In this case the FILELOCATION will be available till you close the program.
Not setting variable will be dependent on OS.
For Linux or Solaris you can do :
export FILELOCATION=<PATH TO FILE>
In case you are looking for command line parameters then you can use something like this:
java MyProgram PathToFile
There is a better way of doing this java -DFILELOCATION=<PATH_TO_FILE> MyProgram
Edit: As per comment.
Just use ProcessBuilder to set ENV variable in Java code.

calling java methods from a terminal

Suppose I have a java program, myProgram.jar, which I have running on a server. To start the program I would type this into the terminal:
>java -jar myProgram.jar
and the program would continue to run indefinitely. Now what about if the program had a function such as
void processInput(String text){
//process the text
}
and I wanted to SSH into the server and call this function with a particular string? so I could log into my server at any time and alter the state of my program. Is this possible?
This can be done, but not easily.
There are standard ways to achieve what you probably want: MBeans. Take a look at http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html
You'll have a number of options here. The simplest would be if you only needed to provide your custom text as an argument at startup to the Java program - in which case you any arguments at the end of your java command would be passed as a String array into your programs main method.
Otherwise, you'll be looking to to implement some sort of remote procedure call (RPC). You could use something like Java RMI (remote method invocation) - where your main execution of your program starts, and you could use child executions of your program (or another client library all together) that calls methods within your main execution while it is still running. If you wanted to extend this further, you could have it host web services over standard HTTP, and use SOAP or REST calls.
There are many additional options and variations here, depending upon your exact requirements.
+1 for DagR's suggestion - again, depending upon exactly what you're looking to do, JMX may be a good fit for this as well.
You could implement a Java client application that calls methods on the Java application running on the server using RMI. Then when you ssh into the server you can run your client application with the parameters you need and have it call the methods in the other program.

Categories

Resources