Variable not working in shell script - shell

I have a source code :
#!/bin/bash
CLASSPATH=Test/bin/
java -cp ${CLASSPATH} "test.shell.com.RunTest"
That code return the error with content as bellow : Error: main class test.shell.com.RunTest could not be found or could not be loaded
But when I change it to :
#!/bin/bash
java -cp Test/bin/ "test.shell.com.RunTest"
It return the correct result of RunTest class.
I try change CLASSPATH to other name but it still error. Please help.

Related

Getting a date in properties file in string format

I want to get a date from a key dynamically which will be used by java program to perform some tasks.
I have to get values from property file to java. cannot do vice versa
So basically the value for this key, job.date=2022-03-23 i can get through date -d tomorrow "+%Y-%m-%d". But this works fine when job.date is accessed from shell script and gives a parsing error when accessed from java class.
so looking for java understandable snippet, or a way to override it while executing the java class with jar
You should use (if this is the property and value you want)
java -Djob.date=$(date -d tomorrow +"%Y-%m-%d") ...
java -Djob.date=$(date -d tomorrow +"%Y-%m-%d") ...
The above gave me exception, error loading main class.
It worked with following snippet:
sed -i "s/\(testauto\.as\.of\.date=\).*\$/\1${tomorrow}/" abc.properties
Added this line in shell script that was calling the java class.

Kotlin - System.getProperty cannot resolve command line argument

I have some code to load application.properties dynamically:
fun loadDefaultProperties(): Properties {
val configPath = System.getProperty("spring.config.location")
val resource = FileSystemResource(configPath)
return PropertiesLoaderUtils.loadProperties(resource)
}
But when I run the command...
java -jar my.jar -Dspring.config.location=my/location/application.properties
...System.getProperty("spring.config.location") returns null, and therefore, I get an IllegalArgumentException because the path is null.
Why am I unable to read the argument from the command line?
You're passing them in the wrong sequence. Pass them like:
java "-Dspring.config.location=my/location/application.properties" -jar my.jar
Otherwise they are program arguments. I've just tested it, and on MacOS, both the above as well as
java -Dspring.config.location=my/location/application.properties -jar my.jar
(without quotes) work.
Don't you need quotes?
java -jar my.jar -Dspring.config.location="my/location/application.properties"

Hadoop command line -D options not working

I am trying to pass a variable (not property) using -D command line option in hadoop like -Dmapred.mapper.mystring=somexyz. I am able to set a conf property in Driver program and read it back in mapper.
So I can use this to pass my string as additional parameter and set it in Driver. But I want to see if -D option can be used to do the same
My command is:
$HADOOP_HOME/bin/hadoop jar /home/hduser/Hadoop_learning_path/toolgrep.jar /home/hduser/hadoopData/inputdir/ /home/hduser/hadoopData/grepoutput -Dmapred.mapper.mystring=somexyz
Driver program
String s_ptrn=conf.get("mapred.mapper.regex");
System.out.println("debug: in Tool Class mapred.mapper.regex "+s_ptrn + "\n");
Gives NULL
BUT this works
conf.set("DUMMYVAL","100000000000000000000000000000000000000"); in driver is read properly in mapper by get method.
My question is if all of Internet is saying i can use -D option then why cant i? is it that this cannot be used for any argument and only for properties? whihc we can read by putitng in file that i should read in driver program then use it?
Something like
Configuration conf = new Configuration();
conf.addResource("~/conf.xml");
in driver program and this is the only way.
As Thomas wrote, you are missing the space. You are also passing variable mapred.mapper.mystring in your CLI, but in the code you are trying to get mapred.mapper.regex. If you want to use -D parameter, you should be using Tool interface. More about it is here - Hadoop: Implementing the Tool interface for MapReduce driver.
Or you can parse your CLI arguments like this:
#Override
public int run(String[] args) throws Exception {
Configuration conf = this.getConf();
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
while (i<otherArgs.length) {
if (otherArgs[i].equals("-x")) {
//Save your CLI argument
yourVariable = otherArgs[++i];
}
//then save yourVariable into conf for using in map phase
Than your command can be like this:
$HADOOP_HOME/bin/hadoop jar /home/hduser/Hadoop_learning_path/toolgrep.jar /home/hduser/hadoopData/inputdir/ /home/hduser/hadoopData/grepoutput -x yourVariable
Hope it helps
To use -D option with hadoop jar command correctly, given below syntax should be used:
hadoop jar {hadoop-jar-file-path} {job-main-class} -D {generic options} {input-directory} {output-directory}
Hence -D option should be placed after job main class name i.e at third position. Because when we issue hadoop jar command then, hadoop scripts invokes RunJar class main(). This main () parses first argument to set Job Jar file in classpath and uses second argument to invoke job class main().
Once Job class main () is called then control is transferred to GenericOptionsParser which first parses generic command line arguments (if any) and sets them in Job's configuration object and then calls Job class' run () with remaining arguments (i.e input and output path)

How do you create a Bash function that accepts files of a specific type as arguments?

So far, I know that you have to create a function in order to pass arguments.
However, how do you denote the type of the argument?
For instance, if you want to compile a Java class file and then run the resulting Java file (without having to type the file name twice to distinguish between the extensions each time), how do you let the function know that the names belong to files of different types?
Let's say this is our function:
compileAndRun()
{
javac $1
java $2 # basically, we want to make this take the same argument
# (because the names of the *.class and *.java files are the same)
}
So, instead of typing:
compileAndRun test.class test.java
We wanna just type this:
compileAndRun test
Any help along with any extraneous information you wanna throw in would be much appreciated.
Just use $1 twice. It is safer to connect the two commands with &&, so java is not run if the compilation is not successful.
function compile_n_run () {
javac "$1".java && java "$1".class
}
Arguments to bash functions don't really have types; they are just strings, and it's up to you to use them appropriately. In this case, it's fairly simply to write a function which takes a Java source file, compiles it, and runs the resulting output.
compile_n_run () {
source=$1
expected_output="${source%.java}.class"
javac "$source" && java "$expected_output"
}
$ compile_n_run test.java
I chose to require the full Java source name because it's a little friendlier with auto-completion; you don't have to remove the .java from the command-line, rather you let the function do that for you. (And otherwise, this answer would be identical to choroba's).

Cakephp shell :Shell class HelloShell could not be found

I am a newer to cakephp .I config the cakephp shell as the cakephp
handbook says,when I run the HelloShell with the command cake
Hello ,I got the error information as follows:
Error: Shell class HelloShell could not be found.
1#G:\htdocs\cakedemo\lib\Cake\Console\ShellDispatcher.php(191):ShellDispatcher>_getShell('hello')
2#G:\htdocs\cakedemo\lib\Cake\Console\ShellDispatcher.php(69):ShellDispatcher->dispatch()
3#G:\htdocs\cakedemo\app\Console\cake.php(33):ShellDispatcher::run(Array) {main}
my cakephp version :
Welcome to CakePHP v2.2.0-beta Console
App : Console
Path: G:\htdocs\cakedemo\app\Console\
anyone who is helpful can give me a advice,plea.
there is your mistake.
you should always be in your APP path to execute the cake console.
...app/>../lib/Cake/Console/cake MyShell
or (using the APP Console folder):
...app/>Console/cake MyShell
and MyShell should then be in ...app/Console/Command/.
Thats all there is to it.
Error: Shell class HelloShell could not be found appear because: typo mistake or run command at wrong directory.
Solution:
1. Setup path for php.exe, cake.exe
2. For example, my Cake website root is:
C:\tools\xampp1.8.3\htdocs\cakephp-2.5.5
Create new file in folder C:\tools\xampp1.8.3\htdocs\cakephp-2.5.5\app\Console\Command\HelloShell.php with content:
class HelloShell extends AppShell {
public function main() {
$this->out('Hello world.');
}
}
3. Open cmd, type:
cd /d C:\tools\xampp1.8.3\htdocs\cakephp-2.5.5\app
cake hello
We use hello in command line to call HelloShell class, because "Convention over configuration".
Reference:
http://book.cakephp.org/2.0/en/console-and-shells.html#creating-a-shell
Make sure you give cake folder path in /var/www/html/Console/cake.php
ini_set('include_path', $root . PATH_SEPARATOR . 'Cake' . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
Then go to root folder. In my case the location will be
/var/www/html/
then give the shell file name; hello is my shell name, that would be
/var/www/html/Console/cake hello
combining together
/var/www/html$ /var/www/html/Console/cake hello
Your shell will be executed.

Resources