In a batch file, you can prompt for values to environment variables using the format of set /p TestVar=Give me a string to save!
and retrieve it using %TestVar%
. However, when I try to do this in Java or C# and trying to immediately use it does not work:
cmd.exe /K "set /p TestVar=Give me a string to save! && ECHO %TestVar%"
the above code also only ECHOs the literal string "%TestVar%"
rather than the string that was set to the TestVar
variable.
I want to use that to set environment variables in Java and C# (and I don't want to use built in stuff like How do I get and set Environment variables in C#?)
For a Java example,
public class visibleCMD {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"set /p TestVar=Give me a string to save! && ECHO %TestVar%\"");
} catch (Exception e) {
System.out.println("error");
}
}
}
the above code only ECHOs %TestVar%, rather than the string that was set to TestVar, almost as if it just isn't being parsed as a variable at all.
For a C# example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisibleCMD
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process.Start("CMD.exe",
"/K set /p TestVar=Give me a string to save! && ECHO %TestVar%");
}
}
}