Compile a java file using docker with own path - java

Hy. I'm trying to compile a .java file using docker. I read the files on docker's website, also I read these links:
docker's website
about volumes
and another question I had put up for gcc compiler
I understood the concept for the gcc compiler since it doesn't create any extra file for compiling.
But the java one does. It creates a Main.class file on my /home directory if I use the following command and compile a file named Main.java
sudo docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java:7 javac Main.java
after learning from the above links I was able to successfully compile a java file with my own path using:
docker run --rm -v /mypathhere/mycode.java:/mycode.java: java:7 javac mycode.java"
if there is any error it shows an error but if there isn't it just compiles and gives me no output, and that's justified because it creates a Main.class file.
My problem is that I am unable to find that Main.class file. I don't know where docker is creating it and I have zero understanding for it. Please help me out.

The .class file will be inside the container, under the root directory.
The best plan is to mount the whole source directory and have javac put the result to the same directory e.g:
docker run --rm -v /mypathhere:/mycode java:7 sh -c "cd mycode; javac mycode.java"
That way, you should get the class file written to the mypathhere directory.
Apologies if that doesn't quite work - it's off the top of my head. Hopefully you get the idea though.

Related

Simple Java Application in Docker - Could not find or load Main class

I have created a simple Eclipse project having Main class inside src/com/docker/Main.java.
When i try to run it inside a docker container after building the image, i get the Could not find or load main class error.
Docker image content:
FROM openjdk:8
COPY . /app/
WORKDIR /app
RUN javac src/com/docker/Main.java
CMD ["java", "com.docker.Main"]
I tried several variations of RUN and CMD but nothing worked. I tried with slashes '/' but still did not worked.
CMD ["java", "com.docker.Main.java"]
CMD ["java", "com.docker.Main.class"]
If i run the following command after the javac part, it lists Main.java and Main.class files.
RUN ls src/com/docker/
Well, maybe this picture will explain the parameters little bit more.

How to run jar files sequentially from a shell script

I am trying to run two java application one after other in my docker container.
In my dockerfile i have specified invoker.sh as the entry point.
ENTRYPOINT ["sh", "/opt/invoker.sh"]
Then i use this script to run two jar files.
#!/bin/sh
java -jar loader.jar
java -jar service.jar
but this does not work. It gives
Error: Unable to access jarfile javaimpl-loader.jar
and only the service.jar is executed. When i tried echo $(ls) it shows that both the jar files are there.
but if i change the script to
#!/bin/sh
echo $(java -jar loader.jar)
java -jar service.jar
then both the jars work. Why cant i use the 1st script. any help regarding this highly apreciated.
It appears the first example is being treated as a single line, you could work with that. Also, I would prefer bash to /bin/sh. Like,
#!/usr/bin/env bash
java -jar loader.jar && java -jar service.jar

javac: File Not Found Error in Dockerfile

I am trying to build a docker image for a java app.
I have done the following in the dockerfile :
FROM java:8
RUN javac HelloDocker.java
CMD ["java","HelloDocker"]
When I am trying to build the image it is throwing the following exception :javac: file not found: HelloDocker.java
The HelloDocker.java file and Dockerfile is in the same directory.
Also, when I tried to compile the java file separately (via javac HelloDocker.java), it did not throw any error.
You are using a deprecated image. You should be using openjdk image. See the below
https://hub.docker.com/_/openjdk/
Also you need javac so you should be using the one with jdk tag and not the jre tag.
So try openjdk:8-jdk
Edit-1
Also you need to copy the files inside your Dockerfile. When you use docker build ., then the current directory files as available to you as context but they are not inside image
FROM java:8
WORKDIR /app
COPY HelloDocker.java .
RUN javac HelloDocker.java
CMD ["java","HelloDocker"]
Assuming this is the complete Dockerfile, you need to have a file named HelloDocker.java in your docker image's file system, before you can compile the file using "RUN javac HelloDocker.java" . You may copy the file from your host's file system to you docker image using docker COPY/ADD command.

MapReduce: executing WordCount v1.0

I am trying to learn MapReduce from the official documentation. To make a jar file for WordCount class, the documentation says to run the following command:
javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar -d wordcount_classes WordCount.java
But, I found that my Hadoop directory has no core.jar present. I suppose my Hadoop installation is alright as I can execute the Hadoop shell script from the Bin folder.
If you trying with that:
javac -classpath `hadoop classpath` -d wordcount_classes WordCount.java
Isn't the best practice, I think, but work for me.
Check in your hadoop-1.2.1 folder (as in my case), which you unzipped in "Prepare to Start Cluster" of single node setup. There you would find hadoop-1.2.1-core.jar
That is the file being used to compile here.

How do you get WordCount.java to compile on Cloudera 4?

I'm trying to compile a simple WordCount.java map-reduce example on a linux (CentOS) installation of Cloudera 4. I keep hitting compiler errors when I reference any of the hadoop classes, but I can't figure out which jars of the hundreds under /usr/lib/hadoop I need to add to my classpath to get things to compile. Any help would be greatly appreciated! What I'd like most is a java file for word count (just in case the one I found is bad for some reason) along with the associated command to compile and run it.
I am trying to do this using just javac rather than Eclipse. My main issue either way is what exactly are the Hadoop libraries from the Cloudera 4 install which I need to include in order to get the classic WordCount example to compile. Basically, I need to put the Java MapReduce API classes (Mapper, Reducer, etc.) in my classpath.
I have a script that builds my hadoop classes. Try:
#!/bin/bash
program=`echo $1 | awk -F "." '{print $1}'`
if [ ! -d "${program}_classes" ]
then mkdir ${program}_classes/;
fi
javac -classpath /usr/lib/hadoop/hadoop-common-2.0.0-cdh4.0.1.jar:/usr/lib/hadoop/client/h\
adoop-mapreduce-client-core-2.0.0-cdh4.0.1.jar -d ${program}_classes/ $1
jar -cvf ${program}.jar -C ${program}_classes/ .;
You were probably missing the key jars:
/usr/lib/hadoop/hadoop-common-2.0.0-cdh4.0.1.jar
and
/usr/lib/hadoop/client/hadoop-mapreduce-client-core-2.0.0-cdh4.0.1.jar
If you are running the Cloudera CDH4 Virtual Machine then the following should get you running:
javac -classpath /usr/lib/hadoop/hadoop-common-2.0.0-cdh4.0.0.jar:/usr/lib/hadoop/client/hadoop-mapreduce-client-core-2.0.0-cdh4.0.0.jar -d wordcount_classes WordCount.java
Or you can export environment:
export JAVA_HOME=/usr/java/default
export PATH=${JAVA_HOME}/bin:${PATH}
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar
and use the commands below:
$ bin/hadoop com.sun.tools.javac.Main WordCount.java
$ jar cf wc.jar WordCount*.class
If you are using Eclipse please do add Hadoop packages. you may get it from java2s or any similar sites. I couldn't say without know anything about what you did till now.

Categories

Resources