pass arguments from shell script to java code - java

I am trying to pass two arguments from shell script to java code.
java -classpath *various properties* -Xmx1025m MainClass "$Arg1" "$Arg2"
However, the application is exiting immediately from which I inferred that the arguments are not getting passed to java code.
The application is working fine when I test it in my eclipse using command line args, but when I invoke it from script, its failing. I even printed out the arg1, arg2 values before invoking the java command, they seem to be okay.

You're doing it right.
Get your Java program to start by looping through its arguments and printing them out, to confirm; but the command line is right. You could show the whole script if you'd like more detailed help.

Related

AdminConfig.showAttribute from Java Code

I want to run the following wsadmin command from my java code.
AdminConfig.showAttribute('lp1(cells/iap/nodes/iapnode/servers/server1/server.xml#ListenerPort_1447149354628)', 'stateManagement')
I saw that there is a CommandMgr with AdminCommands, where I can set some parameters.
But I can't get out, which Command is the right one.
Can someone give me a code example, how to write the wsadmin command menitoned above in java-code?

Running file through a program with arguments

My issue here is that I can't seem to pass arguments through my program. I'm not sure how to explain it clearly but here it goes:
I want to be able to run a script file through a custom interpreter with arguments.
For example:
AtomScript.exe main.atom -> Program runs
When I want to run it through a batch file:
main.atom -> Program runs
Now when I want to pass arguments through the program using the AtomScript command in a batch file:
AtomScript.exe main.atom arg1 arg2 -> Program runs with arguments
The arguments are passed. But when I want to use the simple main.atom command to run it with arguments it doesn't work:
main.atom arg1 arg2 -> Program runs without arguments
I'm not sure what to do at this point. Any suggestions?
main.atom is not an executable, so it can't take any parameters
Windows knows, which program has to be started and does so (ignoring any parameters)
Only if you start the executable directly, you can define parameters (the first one being main.atom)
for example:
C:\>assoc .txt
.txt=txtfile
C:\>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
assoc defines, what filetype that extension is ("txtfile")
ftype defines, what has to be done for this filetype (start notepad)
You can see, there is just one parameter %1 (the filename (main.atom)).
Of course you could expand it with more parameters (needs to be run as administrator):
ftype txtfile=%SystemRoot%\system32\NOTEPAD.EXE %*
(which makes not much sense with this example), but I don't recommend it (it's just done on your computer). Better use the "official way": in the batchfile do exactly the same as on the commandline: start the executable with the desired parameters:
AtomScript.exe main.atom arg1 arg2

How to run JUnit from command line with vm arguments?

Is there any way to run JUnit test cases with program arguments, VM arguments and working directory?
I've tried with org.junit.runner.JUnitCore, but I can't find the way to pass these parameters.
You can pass system parameters with the -Dproperty=value
Program arguments are just passed at the end of the java command line. However, they will be passed to Junit's main method. so you cannot use them in your test class. If you want to use some parameter in your test class, set a system parameter and retrieve it with System.getProperty(String) in your code.
So the command line would look like this (you probably need to set other options like classpath):
java -Dfile.encoding=UTF-8 -Dmy.param=foo org.junit.runner.JUnitCore org.package.MyTest
For the working directory I would change to the desired directory with cd before invoking JUnit.

what's the purpose of using run following a main class on the java command line?

I am seeing some code are started like
java MainClass run -cp ******
I have two questions here,
why using run
why specify another classpath following run, any reason or benefit?
In your example, "run" is only an argument of the program, not the JVM. It is not a keyword or anything. The program will simply be passed it as a String.
The -cp argument seems to be also an argument to the program.
From the look of the command line, I guess the MainClass program is used to run another class, which is looked for in the classpath defined by the -cp argument. So, neither "run" nor "-cp ..." are actually used by the VM to run MainClass, but by MainClass itself to run another program.
run, -cp, and ****** will be passed as arguments to the main function in the java class MainClass.
Anything on the command-line, after the class name, are arguments to the class, not arguments to the java VM.

What is the difference between calling a script from a shell/Terminal and using Java Processes?

I've been having real problems trying to get a ruby script to run through Java. I've had all kinds of solutions proposed, and all of them are failing for some reason, so I'm trying to simplify my problem.
Let's say I have a shell script that just has this line in it:
ruby -rubygems script/test_s2t.rb
At the terminal, I can run this script using script/runruby.sh and it works as expected. Now let's say I have a Java method that does the following:
String[] cmd = {"script/runruby.sh"};
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
This doesn't work (it throws an error back from the Ruby script, specifically, but this is a misdirect because it's really down to the script itself not working as expected). My question is not why that test_s2t.rb script doesn't work, because I think that might be distracting me from the real problem.
My question is simply what is different when I run something through ProcessBuilder as opposed to just running it via the command line. Is it a permissions thing? Path differences? There must be something screwing around with the environment the script runs in, because I can't see a problem with the script itself.
As alwyas, any suggestions appreciated. Three days and counting on this issue...
EDIT - For those curious, the exact error I receive in Java is the one described at the bottom of this question: Java receives an error executing Ruby script; Terminal doesn't
The outcome we got in that question was that I should try JRuby, but that resulted in further problems as I can't get the gems to work properly within JRuby. So I went back to asking myself why it wouldn't run normally in the first place.
The reason I think the error is a distraction is because the error is given simply because it processed a string it wasn't expecting to see. The string it expects is the normal process the script runs, which is using ffmpeg and suchlike. What this means is that the script encountered another error (which it isn't showing, which means it was probably not caused by ruby/jruby but by the processes the script launches like ffmpeg).
It's incredibly frustrating, purely because it runs so perfectly from the command line.
I've run into similar problems and there are two things that seem to be common problems:
1) The environment of the child process will be the same as environment of the current virtual machine. This includes the working directory of the launched process.
Example from: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");
Alternatively, you could set the environment inside the shell script.
2) Do you have the proper shebang #! at the beginning of the .sh file? Personally I'd make it absolutely clear and perhaps explicitly call bash or zsh or whatever with the path to the shell script as the first argument OR directly call ruby with the '-rubygems' and 'script/test_s2t.rb' as arguments.
Good Luck!

Categories

Resources