Jython CLASSPATH with *.jar syntax fails with ImportError? - java

I have a jython application that uses some libraries from org.apache....
I've noticed that when I set the CLASSPATH=/some/path/*.jar, the script fails with an ImportError, whereas if i set the CLASSPATH=/some/path/*, the script will work fine.
unset CLASSPATH
jython /path/to/script.py
This fails with an ImportError: No module named apache
export CLASSPATH=/path/to/jars/*.jar
echo $CLASSPATH # shows all the jars in the folder
jython /path/to/script.py
This fails with the same ImportError
export CLASSPATH=/path/to/jars/*
echo $CLASSPATH # shows an identical list of jars as the previous classpath
jython /path/to/script.py
This succeeds.
Is there something I'm missing about either the CLASSPATH environment variable or about jython? For regular java, sometimes I will set -cp /path/to/jars/*.jar and that works. For regular java, I haven't used the CLASSPATH environment variable.
Edit:
If I diff the echo $CLASSPATH for both of the ways I set the classpath, there is no difference
export CLASSPATH=/path/to/jars/*.jar
echo $CLASSPATH >> first.txt
export CLASSPATH=/path/to/jars/*
echo $CLASSPATH >> second.txt
diff first.txt second.txt
The result of this grep command shows nothing:
ls -1 /path/to/jars/ | grep -v \.jar$

Related

How do i fix this .bat file so it can run in macOS?

I have this .bat file created and working perfectly in Windows. When I tried to run this file from macOS terminal, it shows some error.
I've already had JRE installed in my Mac. I also added:
JAVA_HOME=/Library/Java/Home
export JAVA_HOME;
to my .profile file. All the jars needed are also in a folder beside the .bat file.
This is what's inside the .bat file:
shell
"%JAVA_HOME%\bin\java.exe" -cp .;libs/*;api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
I tried deleting the 'java.exe' from code above, but the error still showed up.
I expect to run this .bat file perfectly.
When it comes to setting JAVA_HOME (on macOS) it's better to use
export JAVA_HOME=$(/usr/libexec/java_home)
inside your ~/.profile. You can also pick any version you like by using -v option.
To list all JVM installations, call:
/usr/libexec/java_home -V
to select one of them, use
export JAVA_HOME=$(/usr/libexec/java_home -v version)
then, you can use it like this
$JAVA_HOME/bin/java -cp .:libs/*:api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
turning .BAT to .sh
you can also create a wrapper script like this
#!/bin/bash
export JAVA_HOME=$(/usr/libexec/java_home)
$JAVA_HOME/bin/java -cp .:libs/*:api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
Make sure to make it executable
chmod +x script.sh
Then, you can call it following way
./script.sh

jar xf file.jar not working in windows 10

Not working in Windows 10:
> jar xf file.jar
'jar' is not recognized as an internal or external command, operable program or batch file.
In a PowerShell, you should first:
$Env:Path+="C:\Program Files (x86)\Java\jre1.8.0_181\bin"
Change jre1.8.0_181 according to your installed Java Runtime Environment. For sanity check, you can print the updated path:
$Env:Path
Then:
java -jar -h
I still haven't figured how to get the xf subcommand to work though.
Set the JAVA_HOME in Environment variable.
Then set path.
then open a new Command prompt and type
$ javac -version
It should print something like that
javac 1.8.0_152
Now try

Why isn't command line not finding finding my .java files?

I am using Windows CMD, and for some reason, I am getting this error message relating to my .java files. For example, I am typing:
javac FirstProgram.java
However, this error message occurs:
javac: file not found: FirstProgram.java
Usage: javac <options> <source files>
use -help for a list of possible options
I typed in javac -version, and I am currently using javac 1.8.0_144. Someone on in another Stack Overflow question suggested to change the System Variables. I used JAVA_HOME as the variable name, and I copied the path to my JDK folder, but thus far, I haven't had much luck. I still receive the same error message.
There are several problems here ... including why
PROBLEM 1:
"...I am not even allowed to save my FirstProgram.java in the Java
folder in Documents." <= ?!?
PROBLEM 2:
"...I got the error message about a false flag" <= This is probably a space " " in the path name
STRONG SUGGESTION:
Download Eclipse and try compiling and running your program from Eclipse. In other words, using an IDE, instead of the command line.
You can download Eclipse here:
http://eclipse.org
There is a good "starters tutorial" here:
Creating your first Java project
Run the dir or dir/p command to see your directory contents on your command prompt
c:\path\to\your program directory\dir
See if FirstProgram.java is listed or not? If not then you are in a wrong directory.
Now you have two options
You change to the correct directory using cd command or
You use the absolute path for your FirstProgram.java file
Navigate to that specific folder containing that program then [ Shift + L_Click ], click open cmd, then run it again to ensure it is being ran in that folder.
You should try to use absolute path while using the command if it says the file isn't found.
javac /some/directory/path/to/the/file/FirstProgram.java
Note: On command line, most of the shells would anyway not let you complete the path if the file doesn't exist there. And the other way if you're copying the path from an explorer/finder, it shall be guaranteed to be existing.
Edit: The absolute path as pointed out in comments would be using forward slashes in Windows, e.g :
javac \some\directory\path\to\the\file\FirstProgram.java
This is my a simplified DOSJavaIDE environment for simple test applications if Eclipse is too much of a hassle. I can point compiling and running an application to a specific JVM version. Study this script to see how folder structure and paths are given in each of the commands.
Folders and files
c:\projects\test1\classes\
c:\projects\test1\lib\
c:\projects\test1\lib\somelib1.jar
c:\projects\test1\lib\somelib2.jar
c:\projects\test1\src\
c:\projects\test1\src\test\GameLoop2.java
c:\projects\test1\src\META-INF\MANIFEST.MF
c:\projects\test1\javaenv.bat
javaenv.bat
#REM Standalone JavaDosEnvironment
#set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
#"%JAVA_HOME%\bin\java" -version
#set cmd=%1
#if "%cmd%"=="" (
#echo Please specify command to run ^(1..n or empty to exit^)
#echo 1=Compile, 2=Jar, 12=CompileJar
#echo 3=Run-test1 GameLoop2 with vsync
#SET /p cmd="1..n: "
)
#IF /I "%cmd%"=="1" set cmd=compile
#IF /I "%cmd%"=="compile" call :COMPILE
#IF /I "%cmd%"=="2" set cmd=jar
#IF /I "%cmd%"=="jar" call :JAR
#IF /I "%cmd%"=="12" set cmd=compilejar
#IF /I "%cmd%"=="compilejar" (
call :COMPILE
call :JAR
)
#IF /I "%cmd%"=="3" set cmd=run-test1
#IF /I "%cmd%"=="run-test1" call :RUN-test1
#goto :END
:COMPILE
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
set cp=./lib/somelib1.jar;./lib/somelib2.jar
"%JAVA_HOME%\bin\javac" -classpath "%cp%" -sourcepath ./src -d ./classes ./src/test/*.java
#goto :eof
:JAR
xcopy /Y .\src\META-INF\*.* .\classes\META-INF\
SET MF=./classes/META-INF/MANIFEST.MF
"%JAVA_HOME%\bin\jar" cvfm ./lib/test.jar %MF% -C ./classes .
#goto :eof
:RUN-test1
"%JAVA_HOME%\bin\java" -cp "./lib/*" test.GameLoop2 "fullscreen=false" fps=60 vsync=true
#goto :eof
:END
#pause
MANIFEST.MF
Implementation-Title: testapp
Implementation-Version: 1.0.0 (2017-07-21)
Implementation-Vendor: myname
Implementation-URL: http://my.homepage.com/
Run this script in a command-line such as javaenv.bat compile, javaenv.bat jar, javaenv.bat run-test1 or run without arguments to prompt for selection list.
See a customized manifest where you may write anything you want and is included in a ./lib/test.jar file. Compile target has few 3rd party dependency libraries in a classpath.

BASH to BATCH java

There's this BASH file:
#!/bin/bash
set -eu
DIR=`dirname $0`
JAR=$DIR/myjar.jar
CLASSPATH=$JAR:./
HEAP_SIZE=-size
java $HEAP_SIZE -cp $CLASSPATH something.something2 "$#"
That I want to turn into a .bat file to run on windows
This is what I have so far:
#ECHO OFF
set DIR = %cd%
set JAR = DIR/myjar.jar
set CLASSPATH = %JAR%:./
set HEAP_SIZE = -size
java %HEAP_SIZE% -cp %CLASSPATH% something.something2
How would I complete it to have the same behavior as the bash file?
%CD% can work, but your bash script seems to be setting the directory based off the path of the script (i.e., argument in $0) and not the current directory. To use the same directory as the batch file, you use %~dp0.
You missed % around your DIR variable when you wanted to expand the value.
You shouldn't have spaces around equal signs in your set statements.
The equivalent of $# in batch is %*.
You should quote your %CLASSPATH%. On Windows you are much more likely to encounter paths with spaces in them.
#ECHO OFF
set "DIR=%~dp0"
set "JAR=%DIR%/myjar.jar"
set "CLASSPATH=%JAR%:./"
set "HEAP_SIZE=-size"
java %HEAP_SIZE% -cp "%CLASSPATH%" something.something2 %*

How to run jar from .conf?

I've got the following script to run:
# harvest_bug
#
start on runlevel [345]
script
java -jar /home/admin/es09AndroidUpdater/es09AndroidUpdater.jar
end script
But my linux machine doesn't know anything about java. If I run java -version, I will get
bash: java: command not found
I guess java is not in $PATH, because I still can run Tomcat and things like that. For example Tomcat's setenv.sh looks like that:
export JAVA_HOME=/usr/java/default
export JRE_HOME=/usr/java/default/jre
PATH=$PATH:$JRE_HOME/bin
export PATH
PATH=$PATH:$JAVA_HOME/bin
export PATH
export JAVA_OPTS="-Des09.config=/home/es09/es09.properties -Xms128m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"
So how can I modify the script to run my jar? Can I do this?
# harvest_bug
#
start on runlevel [345]
script
export JAVA_HOME=/usr/java/default
export JRE_HOME=/usr/java/default/jre
PATH=$PATH:$JRE_HOME/bin
export PATH
PATH=$PATH:$JAVA_HOME/bin
export PATH
export JAVA_OPTS="-Des09.config=/home/es09/es09.properties -Xms128m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"
java -jar /home/admin/es09AndroidUpdater/es09AndroidUpdater.jar
end script
Is that ok? Will linux run this script? Sounds very stupid, because I'm not familiar with bash, linux and things like that.
The main purpose of setting java path in $PATH or $JAVA_HOME environment variable is to define the exact path of java executable.
To run your script, use
$JRE_HOME/bin/java $JAVA_OPTS -jar /home/admin/es09AndroidUpdater/es09AndroidUpdater.jar

Categories