Java command not found for SBT - java

I am working on a docker image which I need to install sbt1.8.2. I downloaded the sbt1.8.2.zip file and unzip it, however while checking the version of sbt using sbtVersion, java is missing even though I declared the JAVA_HOME.
This is my sample dockerfile:
ENV SBT_VERSION=1.8.2
ENV JAVA_VERSION=1.8.0_60
ENV JAVA_HOME /opt/jdk${JAVA_VERSION}/jre/
# Install SBT
RUN curl -L -o sbt-$SBT_VERSION.zip download/internal/url/sbt-1.8.2.zip
RUN unzip -o sbt-$SBT_VERSION.zip -d /opt/
CMD /opt/sbt/bin/sbt run \
sbt sbtVersion
This is my sample bats file for testing.
#!/usr/bin/env bats
load $(pwd)/helpers/sdp-helper.bash
#test "SBT Version" {
EXPVER="1.8.2"
run ${dockerRun} bash -c "JAVA_HOME=/opt/jdk1.8.0_60/jre /opt/sbt/bin/sbt sbtVersion"
sdp::outCmd2Tap
[[ ${status} -eq 0 ]]
[[ ${lines[0]} =~ "${EXPVER}" ]]
}
Error:
/opt/sbt/bin/sbt: line 460: java: command not found
copying runtime jar...
mkdir: cannot create directory '': No such file or directory
/opt/sbt/bin/sbt: line 467: java: command not found
/opt/sbt/bin/sbt: line 229: exec: java: not found

Related

Getting "No such file or directory" for java installation when trying to build a hbase docker image

I'm trying to dockerize a hbase setup on a devcloud instance but I am getting this error when I'm trying to run the start-hbase.sh file
/hbase-2.4.7/bin/hbase: line 794: /jdk1.8.0_281/bin/java: No such file or directory
/hbase-2.4.7/bin/hbase: line 794: /jdk1.8.0_281/bin/java: No such file or directory
I checked and I saw that the path is available so I'm not sure why I'm getting this error.
Below are the contents of my Dockerfile:
FROM alpine:3.8
USER root
RUN passwd -u root
RUN apk update \
&& apk add ca-certificates wget \
&& update-ca-certificates # This line may not do anything
# unlock root
RUN apk add bash
RUN wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" https://javadl.oracle.com/webapps/download/GetFile/1.8.0_281-b09/89d678f2be164786b292527658ca1605/linux-i586/jdk-8u281-linux-x64.tar.gz
RUN tar -xvf jdk-8u281-linux-x64.tar.gz
ENV JAVA_HOME jdk1.8.0_281
ENV PATH $PATH:$JAVA_HOME/bin
# Install Apache HBase
ENV HBASE_VER 2.4.7
RUN wget https://dlcdn.apache.org/hbase/2.4.7/hbase-2.4.7-bin.tar.gz
RUN tar -xvf hbase-2.4.7-bin.tar.gz
RUN "./hbase-2.4.7/bin/start-hbase.sh"
You are better defining a working directory for your main application layer which is HBase in this case and separate your dependencies in explicitly defined absolute paths.
In particular, you should copy your JDK extracted folder to an absolute path:
RUN tar -xvf jdk-8u281-linux-x64.tar.gz
RUN mv ./jdk1.8.0_281 /usr/lib/jvm/
ENV JAVA_HOME /usr/lib/jvm/jdk1.8.0_281
ENV PATH $PATH:$JAVA_HOME/bin

Reading a file path in a bash commad

I have this script:
#! /bin/bash -
# ENVIRONMENT VARIABLES
JDK_PATH="C:\Program Files\Java\jre1.8.0_151\bin\java"
WEKA="C:\Program Files\Weka\weka-3-4\weka.jar"
# ----------------------------------
COMMONS_CLI=./lib/commons-cli-1.0.jar
MRC=./lib/jmrc.jar
LIBS=.:$WEKA:$COMMONS_CLI:$MRC:bin/
$JDK_PATH -Xmx512m -classpath $LIBS recognizer.PersonalityRecognizer $*
From this project
And I try to run this command in git bash command line
./PersonalityRecognizer -i ../output_dir -d -t 2 -a ../mairesse_Apache.arff
However I receive this error:
$ ./PersonalityRecognizer -i ../output_dir -d -t 2 -a ../mairesse_Apache.arff
./PersonalityRecognizer: line 15: C:\Program: No such file or directory
Is there any problem with the space character in Java path? How can I write it properly
Your command should be:
"$JDK_PATH" -Xmx512m -classpath "$LIBS" recognizer.PersonalityRecognizer $*
ot bash will see spaces as separators not as part of the path

Docker image openjdk:8-jdk-alpine fails to execute a simple command

I created a docker image from openjdk:8-jdk-alpine using the below Dockerfile:
But when I try to execute simple commands I get the following errors:
/bin/sh: ./run.sh: not found
my run.sh looks like this
enter image description here
I try to "docker run -it [images] bash" enter to the interactive environment,I can see the file "run.sh".In the directory /bin bash exist,but I execute run.sh also display " /bin/sh: ./run.sh: not found"
PS:Sorry for my poor english,I am a chinese student
The printed content of the run.sh, indicates that my original assessment was incorrect; however based on the error message, and the image of the run.sh file, I have a lead.
Your run.sh script has an exec line of #!/bin/sh, which means that it does not need bash to operate so my previous assessment was incorrect.
Starting on a mac, I created a run.sh script, duplicated the dockerfile (mostly), and it ran correctly, producing a valid run.
I then converted the run.sh to use dos line endings and got the following:
$ file run.sh
run.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators
$ docker run --rm -it bob
/bin/sh: ./run.sh: not found
Which looks suspiciously like your error message.
From this, it would lead me to believe that your run.sh file contains dos line endings. Based on the images, I'm guessing that you're on windows, which is where the problem with the run.sh script originates.
how to convert the line endings (some examples):
dos2unix run.sh
perl -pi -e 's/\r\n/\n/g' run.sh
Previous Answer
The most likely reason for this issue is that the shebang line in the run.sh contains: #!/usr/bin/bash, or something of that ilk - i.e. it doesn't reference the valid path to the binary that will run the shell script.
On alpine, bash is installed into /bin, so if you try to run the script you will see the error:
/ # ./run.sh
/bin/sh: ./run.sh: not found
/ # cat run.sh
#!/usr/bin/bash
echo "Hi"
workaround (1): after the apk add bash, do an:
RUN ln -s /bin/bash /usr/bin
in your Dockerfile. This will create a symlink for bash, allowing the program to run:
/ # ln -s /bin/bash /usr/bin
/ # ./run.sh
Hi
workaround(2) - if you don't want to make a symlink like this, you can always invoke bash as part of the CMD -
CMD [ 'bash', './run.sh' ]

Error Executing Makefiles on Netbeans 7.1

Am trying to execute a makefile which will automatically runs a compiled front end of a java code i have written. the contents of the make file is as follows:
build: compile test
compile:
javac lexer/*.java
javac symbols/*.java
javac inter/*.java
javac parser/*.java
javac main/*.java
test:
#for i in `(cd tests; ls *.t | sed -e 's/.t$$//')`;\
do echo $$i.t;\
java main.Main <tests/$$i.t >tmp/$$i.i;\
diff tests/$$i.i tmp/$$i.i;\
done
clean:
(cd lexer; rm *.class)
(cd symbols; rm *.class)
(cd inter; rm *.class)
(cd parser; rm *.class)
(cd main; rm *.class)
yacc:
/usr/ccs/bin/yacc -v doc/front.y
rm y.tab.c
mv y.output doc
When i run make from netbeans, i get this error on the terminal:
javac lexer/*.java
Makefile:4: recipe for target `compile' failed
/bin/sh: javac: command not found
make: *** [compile] Error 127
MAKE FAILED (exit value 2, total time: 660ms)
Please how do i solve this problem.?
The error means the Java compiler (javac) cannot be found.
Your JAVA_HOME variable needs to be set and also appended to PATH.
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_06\bin
export Path:=$(JAVA_HOME);$(Path)
Add these lines to the start of your makefile.

Creating a jar file from a Scala file

I'm new to Scala and don't know Java. I want to create a jar file out of a simple Scala file. So I have my HelloWorld.scala, generate a HelloWorld.jar.
Manifest.mf:
Main-Class: HelloWorld
In the console I run:
fsc HelloWorld.scala
jar -cvfm HelloWorld.jar Manifest.mf HelloWorld\$.class HelloWorld.class
java -jar HelloWorld.jar
=> "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/jar"
java -cp HelloWorld.jar HelloWorld
=> Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
at hoppity.main(HelloWorld.scala)
Sample directory structure:
X:\scala\bin
X:\scala\build.bat
X:\scala\MANIFEST.MF
X:\scala\src
X:\scala\src\foo
X:\scala\src\foo\HelloWorld.scala
HelloWorld.scala:
//file: foo/HelloWorld.scala
package foo {
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
}
MANIFEST.MF:
Main-Class: foo.HelloWorld
Class-Path: scala-library.jar
build.bat:
#ECHO OFF
IF EXIST hellow.jar DEL hellow.jar
IF NOT EXIST scala-library.jar COPY %SCALA_HOME%\lib\scala-library.jar .
CALL scalac -sourcepath src -d bin src\foo\HelloWorld.scala
CD bin
jar -cfm ..\hellow.jar ..\MANIFEST.MF *.*
CD ..
java -jar hellow.jar
In order to successfully use the -jar switch, you need two entries in the META-INF/MANIFEST.MF file: the main class; relative URLs to any dependencies. The documentation notes:
-jar
Execute a program encapsulated in a
JAR file. The first argument is the
name of a JAR file instead of a
startup class name. In order for this
option to work, the manifest of the
JAR file must contain a line of the
form Main-Class: classname. Here,
classname identifies the class having
the public static void main(String[]
args) method that serves as your
application's starting point. See the
Jar tool reference page and the Jar
trail of the Java Tutorial for
information about working with Jar
files and Jar-file manifests.
When you use this option, the JAR file
is the source of all user classes,
and other user class path settings are ignored.
java command line usage
manifest spec
(Notes: JAR files can be inspected with most ZIP applications; I probably neglect handling spaces in directory names in the batch script; Scala code runner version 2.7.4.final .)
For completeness, an equivalent bash script:
#!/bin/bash
if [ ! $SCALA_HOME ]
then
echo ERROR: set a SCALA_HOME environment variable
exit
fi
if [ ! -f scala-library.jar ]
then
cp $SCALA_HOME/lib/scala-library.jar .
fi
scalac -sourcepath src -d bin src/foo/HelloWorld.scala
cd bin
jar -cfm ../hellow.jar ../MANIFEST.MF *
cd ..
java -jar hellow.jar
Because Scala scripts require the Scala libraries to be installed, you will have to include the Scala runtime along with your JAR.
There are many strategies for doing this, such as jar jar, but ultimately the issue you're seeing is that the Java process you've started can't find the Scala JARs.
For a simple stand-alone script, I'd recommend using jar jar, otherwise you should start looking at a dependency management tool, or require users to install Scala in the JDK.
I ended up using sbt assembly, it is really simple to use. I added a file called assembly.sbt into the project/ directory at the root of the project with a one liner (Note your version might need to be changed).
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
Then just run the assembly task in sbt:
> assembly
Or just 'sbt assembly' in project root directory
$ sbt assembly
It will first run your tests and then it will generate the new jar into the target/ directory (given that my build.sbt already lists all my dependencies).
In my case, I just make that .jar file executable, rename to remove the extension and it is ready to ship!
Also, if you are doing a command line tool, don't forget to add a man page (I hate scripts without proper manpages or with multi-page plain text documentation that is not even piped into a pager for you).
You can also use maven and the maven-scala-plugin. Once you set up maven, you can just do mvn package and it will create your jar for you.
I tried to reproduce MyDowell's method. Finally I could make it work. However I find that the answer though correct a bit too complicated for a novice ( in particular the directory structure is unnecessarily complicated ).
I can reproduce this result with very simplistic means. To start with there is only one directory which contains three files:
helloworld.scala
MANIFEST.MF
scala-library.jar
helloworld.scala
object HelloWorld
{
def main(args: Array[String])
{
println("Hello, world!")
}
}
MANIFEST.MF:
Main-Class: HelloWorld
Class-Path: scala-library.jar
first compile helloworld.scala:
scalac helloworld.scala
then create the jar:
\progra~1\java\jdk18~1.0_4\bin\jar -cfm helloworld.jar MANIFEST.MF .
now you can run it with:
java -jar helloworld.jar
I found this simple solution because the original one did not work. Later I found out that not because it is wrong, but because of a trivial error: if I don't close the second line in MANIFEST.MF with a newline, then this line will be ignored. This took me an hour to find out and I tried all other things before, in the process finding this very simple solution.
I don't want to write why's and how's rather just show the solution which worked in my case (via Linux Ubuntu command line):
1)
mkdir scala-jar-example
cd scala-jar-example
2)
nano Hello.scala
object Hello extends App { println("Hello, world") }
3)
nano build.sbt
import AssemblyKeys._
assemblySettings
name := "MyProject"
version := "1.0"
scalaVersion := "2.11.0"
3)
mkdir project
cd project
nano plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.9.1")
4)
cd ../
sbt assembly
5)
java -jar target/target/scala-2.11/MyProject-assembly-1.0.jar
>> Hello, world
I modified the bash script adding some intelligence including auto-manifest generation.
This script assumes that the main object is named the same as the file it is in (case sensitive). Also, either the current directory name must equal to the main object name or the main object name should be provided as a command line parameter. Launch this script from the root directory of your project. Modify the variables at the top as required.
Be aware that the script will generate the bin and dist folders and will ERASE any existing contents in bin.
#!/bin/bash
SC_DIST_PATH=dist
SC_SRC_PATH=src
SC_BIN_PATH=bin
SC_INCLUDE_LIB_JAR=scala-library.jar
SC_MANIFEST_PATH=MANIFEST.MF
SC_STARTING_PATH=$(pwd)
if [[ ! $SCALA_HOME ]] ; then
echo "ERROR: set a SCALA_HOME environment variable"
exit 1
fi
if [[ ! -f $SCALA_HOME/lib/$SC_INCLUDE_LIB_JAR ]] ; then
echo "ERROR: Cannot find Scala Libraries!"
exit 1
fi
if [[ -z "$1" ]] ; then
SC_APP=$(basename $SC_STARTING_PATH)
else
SC_APP=$1
fi
[[ ! -d $SC_DIST_PATH ]] && mkdir $SC_DIST_PATH
if [[ ! -d $SC_BIN_PATH ]] ; then
mkdir "$SC_BIN_PATH"
else
rm -r "$SC_BIN_PATH"
if [[ -d $SC_BIN_PATH ]] ; then
echo "ERROR: Cannot remove temp compile directory: $SC_BIN_PATH"
exit 1
fi
mkdir "$SC_BIN_PATH"
fi
if [[ ! -d $SC_SRC_PATH ]] || [[ ! -d $SC_DIST_PATH ]] || [[ ! -d $SC_BIN_PATH ]] ; then
echo "ERROR: Directory not found!: $SC_SRC_PATH or $SC_DIST_PATH or $SC_BIN_PATH"
exit 1
fi
if [[ ! -f $SC_DIST_PATH/$SC_INCLUDE_LIB_JAR ]] ; then
cp "$SCALA_HOME/lib/$SC_INCLUDE_LIB_JAR" "$SC_DIST_PATH"
fi
SCALA_MAIN=$(find ./$SC_SRC_PATH -name "$SC_APP.scala")
COMPILE_STATUS=$?
SCALA_MAIN_COUNT=$(echo "$SCALA_MAIN" | wc -l)
if [[ $SCALA_MAIN_COUNT != "1" ]] || [[ ! $COMPILE_STATUS == 0 ]] ; then
echo "Main source file not found or too many exist!: $SC_APP.scala"
exit 1
fi
if [[ -f $SC_DIST_PATH/$SC_APP.jar ]] ; then
rm "$SC_DIST_PATH/$SC_APP.jar"
if [[ -f $SC_DIST_PATH/$SC_APP.jar ]] ; then
echo "Unable to remove existing distribution!: $SC_DIST_PATH/$SC_APP.jar"
exit 1
fi
fi
if [[ ! -f $SC_MANIFEST_PATH ]] ; then
LEN_BASE=$(echo $(( $(echo "./$SC_SRC_PATH" |wc -c) - 0 )))
SC_MAIN_CLASS=$(echo $SCALA_MAIN |cut --complement -c1-$LEN_BASE)
SC_MAIN_CLASS=${SC_MAIN_CLASS%%.*}
SC_MAIN_CLASS=$(echo $SC_MAIN_CLASS |awk '{gsub( "/", "'"."'"); print}')
echo $(echo "Main-Class: "$SC_MAIN_CLASS) > $SC_MANIFEST_PATH
echo $(echo "Class-Path: "$SC_INCLUDE_LIB_JAR) >> $SC_MANIFEST_PATH
fi
scalac -sourcepath $SC_SRC_PATH -d $SC_BIN_PATH $SCALA_MAIN
COMPILE_STATUS=$?
if [[ $COMPILE_STATUS != "0" ]] ; then
echo "Compile Failed!"
exit 1
fi
cd "$SC_BIN_PATH"
jar -cfm ../$SC_DIST_PATH/$SC_APP.jar ../$SC_MANIFEST_PATH *
COMPILE_STATUS=$?
cd "$SC_STARTING_PATH"
if [[ $COMPILE_STATUS != "0" ]] || [[ ! -f $SC_DIST_PATH/$SC_APP.jar ]] ; then
echo "JAR Build Failed!"
exit 1
fi
echo " "
echo "BUILD COMPLETE!... TO LAUNCH: java -jar $SC_DIST_PATH/$SC_APP.jar"
echo " "
One thing which may cause a similar problem (although it's not the problem in the initial question above) is that the Java vm seems to demand that the main method returns void. In Scala we can write something like (observe the =-sign in the definition of main):
object MainProgram {
def main(args: Array[String]) = {
new GUI(args)
}
}
where main actually returns a GUI-object (i.e. it's not void), but the program will run nicely when we start it using the scala command.
If we package this code into a jar-file, with MainProgram as the Main-Class, the Java vm will complain that there's no main function, since the return type of our main is not void (I find this complaint somewhat strange, since the return type is not part of the signature).
We would have no problems if we left out the =-sign in the header of main, or if we explicitly declared it as Unit.
If you do not wish to use sbt facilities I recommend the use of a makefile.
Here is an example where foo package is replaced by foo.bar.myApp for completeness.
makefile
NAME=HelloWorld
JARNAME=helloworld
PACKAGE=foo.bar.myApp
PATHPACK=$(subst .,/,$(PACKAGE))
.DUMMY: default
default: $(NAME)
.DUMMY: help
help:
#echo "make [$(NAME)]"
#echo "make [jar|runJar]"
#echo "make [clean|distClean|cleanAllJars|cleanScalaJar|cleanAppJar]"
.PRECIOUS: bin/$(PATHPACK)/%.class
bin/$(PATHPACK)/%.class: src/$(PATHPACK)/%.scala
scalac -sourcepath src -d bin $<
scala-library.jar:
cp $(SCALA_HOME)/lib/scala-library.jar .
.DUMMY: runjar
runJar: jar
java -jar $(JARNAME).jar
.DUMMY: jar
jar: $(JARNAME).jar
MANIFEST.MF:
#echo "Main-Class: $(PACKAGE).$(NAME)" > $#
#echo "Class-Path: scala-library.jar" >> $#
$(JARNAME).jar: scala-library.jar bin/$(PATHPACK)/$(NAME).class \
MANIFEST.MF
(cd bin && jar -cfm ../$(JARNAME).jar ../MANIFEST.MF *)
%: bin/$(PATHPACK)/%.class
scala -cp bin $(PACKAGE).$#
.DUMMY: clean
clean:
rm -R -f bin/* MANIFEST.MF
cleanAppJar:
rm -f $(JARNAME).jar
cleanScalaJar:
rm -f scala-library.jar
cleanAllJars: cleanAppJar cleanScalaJar
distClean cleanDist: clean cleanAllJars

Categories