Setting Java CLASSPATH doesn't work but -cp does - java

So, here's the problem I encountered. I wrote a simple .bat file to run weka on some data sets I had but Java recently updated itself and it stopped working. My old code was this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
This worked before and it did the job I asked of it. However, after the java update, I kept getting the error "Could not find or load main class weka.classifiers.meta.FilteredClassifier". I couldn't figure it out because the directory names and class names were exactly correct. So, I changed the code to this:
#ECHO OFF
SET CLASSPATH = "C:\Program Files (x86)\Weka-3-6\weka.jar"
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI.arff
java -cp "C:\Program Files (x86)\Weka-3-6\weka.jar" weka.classifiers.meta.FilteredClassifier -t %%~nI.arff -F "weka.filters.unsupervised.attribute.Remove -R 1,3,4,5" -W weka.classifiers.functions.LinearRegression -x 10 >> results.txt
ECHO >> results.txt
)
And it worked again. Can anyone tell me why this happened? The only thing I can think of is that the Java update isn't playing nice with itself somehow. Any insight would be appreciated thanks.

SET WEKA_HOME=c:\Program Files (x86)\Weka-3-6
SET CLASSPATH=%CLASPATH%;%WEKA_HOME%\weka.jar
bash learn.sh

Related

can't make a shell script to make a jar file

I try to make a simple shell script to make a jar file. The jar command combined with -C does not work with wildcards. Therefor I use a wildcard to find the files I want. Write them to a file, and loop over them.
It looks something like this:
the_classes=''
cd "$bin_folder"
tmp_dir=$(mktemp -d -t java_sucks)
find "imui/core/" -type f -name "IMUI_Widget_Agent*.class" >"$tmp_dir/classes.txt"
while IFS="" read -r p || [ -n "$p" ]
do
the_classes="${the_classes} -C '$bin_folder' '$p'"
done < "$tmp_dir/classes.txt"
Using the above I complete the command:
cmd='jar cfm build/IMUI_Widget_Agent.jar'
cmd="${cmd} \"$bin_folder/imui/core/IMUI_Widget_Agent_MANIFEST.MF\" $the_classes"
printf "\n\n\ncmd\n\n\n"
echo $cmd
Now if I copy and paste this command to execute it works!
But I want to avoid the manual labour of doing the copy and paste by hand every time.
Now I have:
eval "$("$cmd")"
But I get an error File name too long. No matter what I try, every fix I do creates a new problem. I have been working 6 hours now to make this script.
What would be a good step forward?
Since you cd "$bin_folder" you don't actually need -C "$bin_folder":
#!/bin/bash
shopt -s globstar
cd "$bin_folder"
jar cfm build/IMUI_Widget_Agent.jar \
imui/core/IMUI_Widget_Agent_MANIFEST.MF \
imui/core/**/IMUI_Widget_Agent*.class
However, if you still want to add them as part of a larger script, you can easily and robustly build your command in an array:
#!/bin/bash
shopt -s globstar
cmd=(jar cfm build/IMUI_Widget_Agent.jar imui/core/IMUI_Widget_Agent_MANIFEST.MF)
cd "$bin_folder"
for file in imui/core/**/IMUI_Widget_Agent*.class
do
cmd+=(-C "$bin_folder" "$file")
done
echo "About to execute: "
printf "%q " "${cmd[#]}"
echo
"${cmd[#]}"
Alternatively, you can simply do eval "$cmd" with your code, which is equivalent to echo and copy-pasting. However, be aware that this is fragile and error prone because it requires careful escaping of the filenames which you're not currently doing.

getting the error "TERM environment variable not set" when running a bash script from Java process builder [duplicate]

I have a file.sh with this, when run show : TERM environment variable not set.
smbmount //172.16.44.9/APPS/Interfas/HERRAM/sc5 /mnt/siscont5 -o
iocharset=utf8,username=backup,password=backup2011,r
if [ -f /mnt/siscont5/HER.TXT ]; then
echo "No puedo actualizar ahora"
umount /mnt/siscont5
else
if [ ! -f /home/emni/siscont5/S5.TXT ]; then
echo "Puedo actualizar... "
touch /home/emni/siscont5/HER.TXT
touch /mnt/siscont5/SC5.TXT
mv -f /home/emni/siscont5/CCORPOSD.DBF /mnt/siscont5
mv -f /home/emni/siscont5/CCTRASD.DBF /mnt/siscont5
rm /mnt/siscont5/SC5.TXT
rm /home/emni/siscont5/HER.TXT
echo "La actualizacion ha sido realizada..."
else
echo "No puedo actualizar ahora: Interfaz exportando..."
fi
fi
umount /mnt/siscont5
echo "/mnt/siscont5 desmontada..."
You can see if it's really not set. Run the command set | grep TERM.
If not, you can set it like that:
export TERM=xterm
Using a terminal command i.e. "clear", in a script called from cron (no terminal) will trigger this error message. In your particular script, the smbmount command expects a terminal in which case the work-arounds above are appropriate.
You've answered the question with this statement:
Cron calls this .sh every 2 minutes
Cron does not run in a terminal, so why would you expect one to be set?
The most common reason for getting this error message is because the script attempts to source the user's .profile which does not check that it's running in a terminal before doing something tty related. Workarounds include using a shebang line like:
#!/bin/bash -p
Which causes the sourcing of system-level profile scripts which (one hopes) does not attempt to do anything too silly and will have guards around code that depends on being run from a terminal.
If this is the entirety of the script, then the TERM error is coming from something other than the plain content of the script.
You can replace :
export TERM=xterm
with :
export TERM=linux
It works even in kernel with virgin system.
SOLVED: On Debian 10 by adding "EXPORT TERM=xterm" on the Script executed by CRONTAB (root) but executed as www-data.
$ crontab -e
*/15 * * * * /bin/su - www-data -s /bin/bash -c '/usr/local/bin/todos.sh'
FILE=/usr/local/bin/todos.sh
#!/bin/bash -p
export TERM=xterm && cd /var/www/dokuwiki/data/pages && clear && grep -r -h '|(TO-DO)' > /var/www/todos.txt && chmod 664 /var/www/todos.txt && chown www-data:www-data /var/www/todos.txt
If you are using the Docker PowerShell image set the environment variable for the terminal like this with the -e flag
docker run -i -e "TERM=xterm" mcr.microsoft.com/powershell

YUICompressor - compress multiple input to multiple output

I want to compress multiple input file into multiple output minified files like this :
Input :
file1.css
file2.css
file3.css
Output:
file1-min.css
file2-min.css
file3-min.css
I'm trying to achieve that with the following command line :
java -jar yuicompressor-2.4.8.jar *.css -o $.min.css
It is not helping me at all > it creates a $.min.cs and not file1-min.css etc...
You guys have an idea how to achieve that ?
I have search some answer for this into stackoverflow, without success.
Thanks for your help,
Regards.
Update
Here is the solution :
#echo off
setlocal
echo doing some css min
for %%F in (%cd% *.css) do (
echo %%~nF
java -jar yuicompressor-2.4.8.jar %%F -o min\%%~nF.min.css
echo "done"
)
)
I think you'll need to loop over your css files and call yuicompressor for each item.
For example, in windows batch
FOR /F %G IN ('dir /b C:\yourcssPath\*.css') DO java -jar yuicompressor-2.4.8.jar %G -o %G.min.css

gettext under windows 7 for java project returns 'Bad file descriptor'

I'm trying to use gettext in java project to provide translations for different languages.
My workstation runs Windows 7 and so I'd like to be able to go through whole build process on it (including keys generation and translations update - all from gradle script).
However msgfmt tool returns an error: 'Bad file descriptor' without any additional information...
I was not able to find any helpful information over the Internet.
Someone had similar problem and guy named Daiki Ueno proposed a patch but I guess it didn't go to official gettext versions since the issue is still present...
See: https://lists.gnu.org/archive/html/bug-gnulib/2013-09/msg00049.html
Steps
create list of java files in the project
dir *.java /s/b > files.txt
create translations directory
mkdir po
creates a pot file that contains all strings in the native language
xgettext -ktrc -ktr -kmarktr -ktrn:1,2 --from-code=utf-8 -o po\keys.pot -f files.txt
replace CHARSET in keys.pot file to correct encoding name 'utf-8'
...manual step...
create translations file with content description
type nul >> po\pl.po & copy po\pl.po +,,
echo msgid "" >> po\pl.po
echo msgstr "" >> po\pl.po
echo "Content-Type: text/plain; charset=UTF-8\n\" >> po\pl.po
merge keys into localized po file
msgmerge -U po\pl.po po\keys.pot
create default ResourceBundle class file << fails with error (see below)
set JAVAC=c:\Java\jdk1.8.0_40\bin\javac.exe
set TMPDIR=c:\temp
msgfmt --verbose -java2 -d src\main\java -r com.haso.Messages po\keys.pot
I've tried following implementations of gettext:
GetGnuWin32-0.6.3
MinGW-0.18.3
gettext-tool-windows-0.18.3
gettext-tool-windows-0.19.4
gettext-iconv-windows-0.19.4
first one crashes in Windows 7 64bit
msgfmt from the rest give the following output:
>msgfmt: c:\Java\jdk1.8.0_40\bin\javac.exe subprocess failed: Bad file descriptor
Could anyone help with getting it work?
I was able to find some workaround to my problem since msgfmt downloaded from here generated java sources...
This solution requires following environmental variables set correctly:
JAVA_HOME
CYGWIN_HOME
GETTEXT_HOME
I created following script run_gettext.sh in subdirectory named gettext in my project root dir:
#!/bin/bash
cd `$CYGWIN_HOME/bin/pwd.exe`/gettext
set CYGWIN_HOME=%CYGWIN_HOME%
set GETTEXT_HOME=%GETTEXT_HOME%
$CYGWIN_HOME/bin/rm.exe -fv files.txt
$CYGWIN_HOME/bin/find.exe ../src/main/java -name *.java > files.txt
$CYGWIN_HOME/bin/mkdir.exe -pv po
$GETTEXT_HOME/xgettext.exe -ktrc -ktr -kmrktr -ktrn:1,2 --from-code=UTF-8 -o po/keys.pot -f files.txt
$CYGWIN_HOME/bin/sed.exe -i 's/CHARSET/UTF-8/g' po/keys.pot
if [ ! -f po/pl.po ]; then
$CYGWIN_HOME/bin/cp.exe -v template.po po/pl.po
fi
$GETTEXT_HOME/msgmerge.exe -U po/pl.po po/keys.pot
export JAVAC=`$CYGWIN_HOME/bin/pwd.exe`/javac.sh
$CYGWIN_HOME/bin/rm.exe -rfv tmp
$CYGWIN_HOME/bin/mkdir.exe -pv tmp
export TMPDIR=`$CYGWIN_HOME/bin/pwd.exe`/tmp/
$GETTEXT_HOME/msgfmt.exe --verbose --java2 -d tmp -r com.haso.i18n.Messages po/keys.pot
$GETTEXT_HOME/msgfmt.exe --verbose --java2 -d tmp -r com.haso.i18n.Messages -l pl po/pl.po
$CYGWIN_HOME/bin/rm.exe -rfv ../src/main/java/com/haso/i18n
$CYGWIN_HOME/bin/mkdir.exe -pv ../src/main/java/com/haso/i18n
$CYGWIN_HOME/bin/find.exe tmp -name "Messages*.java" | $CYGWIN_HOME/bin/xargs.exe -n 1 $CYGWIN_HOME/bin/cp.exe -vt ../src/main/java/com/haso/i18n/
and executed it from my project root directory with command:
%CYGWIN_HOME%\bin\bash gettext/run_gettext.sh
where javac.sh is:
#!/bin/bash
#
# A wrapper for calling Javac from Cygwin
# Original Author: Igor Pechtchanski <pechtcha#cs.nyu.edu
# Modified by Florian Gattung <fgattung#gmail.com> for msgfmt compatibility
#
ME="`basename $0`"
JAVAC_EXEC="/cygdrive/%JAVA_HOME%/bin/javac.exe"
ARGS=""
#
while [ -n "$1" ]; do
arg="$1"
shift
if [ -d $arg ]; then
arg="`cygpath -p -w "$arg"`"
fi
if [ -f $arg ]; then
arg="`cygpath -p -w "$arg"`"
fi
ARGS="$ARGS '$arg'"
done
eval "set -- $ARGS"
exec "$JAVAC_EXEC" "$#"
Created *.java files are then compiled by gradle on CI together with project source code.
File template.po contains:
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"

Calling java program dependent on external library

I am trying to call a java program in php to use it with web interface.
Java program is dependent on an external lib: commons-cli-1.2.jar
So basically I need to export it before calling the java program; but if I export it first as:
shell_exec('export CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar');
then call the java program as:
shell_exec('java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o');
I think it creates different shells for each call; then the export does not have any effect on java program. Or am I wrong?
Otherwise, it should output a file in the server. But simply it does not. So, what is wrong? Any idea?
edit: However can it be because some parameters such as para_i stands for an input file name, so that i have to specify full path for that? Because I just assume if the input file is in the same working directory, there won't be any problem, will it?
edit-2: it outputs properly when i use command line;)
you're right, each shell_exec creates a separate shell.
env CLASSPATH=whatever java -switches
I would use
shell_exec('java -cp $CLASSPATH:/home/yourname/dir/lib/commons-cli-1.2.jar ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
and (this is important) replace the tilde(~) with the actual path to your directory (/home/yourname say). The ~ is expanded by the shell and is dependent on which shell you''re using.
Try Creating a simple shell script with the commands that you want to execute. You may pass arguments to a shell script so that is not a problem either.
for example
echo "Running Script..."
java -cp $CLASSPATH:~/lib/commons-cli-1.2.jar ComputePagerank -i $1 -d $2 -e $3 -o $4 > message
etc.
Then try calling it from the command line first with some parameters. Did it output? Then try calling it from the php script. Did it output? If it did not then you may need to check permissions. I had a simiolar experience some time ago with a Java program that simply did not have permission to write a file.
You should be able to call it like this.
shell_exec('java -cp $CLASSPATH:~/lib/commons-cli-1.2.jar ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
Another option is to issue the 2 commands seperately, but to the same shell, like this:
shell_exec('export CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar; java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
edit:
some shells don't let you call export while you're setting up the variable. so this may be safer than the second option above:
shell_exec('CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar; export CLASSPATH; java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');
another edit:
If none of the above work then you're going to have to do some more trouble shooting. Does your java program work from the command prompt?
java -cp $CLASSPATH:/home/user/lib/commons-cli-1.2.jar ComputePagerank -i param1 -d param2 -e param3 -o param4 > message

Categories

Resources