Unable to run java Program. Error :The system cannot find t he file specified or javac is not recognized as internal or external command

Hey… I am just a beginner to Docker and was exploring the various features.i have successfully installed java inside docker

**OS version**:Windows Server 2016

PS C:\testing> docker version
Client:
 Version:      17.03.1-ee-3
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   3fcee33
 Built:        Thu Mar 30 19:31:22 2017
 OS/Arch:      windows/amd64
Server:
   Version:      17.03.1-ee-3

 API version:  1.27 (minimum version 1.24)
 Go version:   go1.7.5
 Git commit:   3fcee33
 Built:        Thu Mar 30 19:31:22 2017
 OS/Arch:      windows/amd64
 Experimental: false
PS C:\testing>

Created a Dockerfilein the path c:\java-windows-docker\Dockerfile, and put the following lines inside:

  FROM windowsservercore  
  RUN powershell (new-object     
System.Net.WebClient).Downloadfile('http://javadl.oracle.com/webapps/download/AutoDL?
BundleId=210185', 'C:\jre-8u91-windows-x64.exe')
RUN powershell start-process -filepath C:\jre-8u91-windows-x64.exe -passthru -wait -argumentlist"/s,INSTALLDIR=c:\Java\jre1.8.0_91,/L,install64.log"
RUN del C:\jre-8u91-windows-x64.exe
CMD [ "c:\\Java\\jre1.8.0_91\\bin\\java.exe", "-version"]

It downloads the Java 8 Update 91 Windows installer and silently installs it to c:\Java\jre1.8.0_91.

docker build -t java-windows-docker c:\java-windows-docker

And if we run it,After start, the container launches Java and prints out its version.

PS C:\Windows\system32> docker run java-windows-docker
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)

We have Java running. inside a Windows docker container which is hosted on a Windows server 2016
I want to run a simple java program.The code within java files are as below:

    class Hello{  
public static void main(String[] args){  
System.out.println("This is java app \n by using Docker");  
} 
} 

Then as the next step I built the image for the above program. Dockerfile for building java program

FROM java-windows-docker 
COPY c:\\testing
WORKDIR C:\\testing
RUN javac Hello.java
ENTRYPOINT ["java"]
CMD ["Hello"]

when i build the image.i get shown the below error:

PS C:\testing> docker build -t testing C:\testing
Sending build context to Docker daemon 11.78 kB
  Step 1/5 : FROM java-windows-docker
 ---> 534f46817a18
   Step 2/5 : WORKDIR C:\\testing
 ---> fc954983c8b0
Removing intermediate container 5e30071d9159
Step 3/5 : RUN javac Hello.java
 ---> Running in c91c62ba707c
javac' is not recognized as an internal or external command,
operable program or batch file.
The command 'cmd /S /C javac Hello.java' returned a non-zero code: 1

Any advise on this would be helpful. Even after installing java inside docker also i’m unable to run simple java program

javac is not in the path. Here’s how to put it there: https://github.com/docker-library/golang/blob/master/1.7/windows/windowsservercore/Dockerfile#L31

Stefan Scherer maintains a bunch of images with Java already installed: https://github.com/StefanScherer/dockerfiles-windows/tree/master/java

I set the below Path

FROM microsoft/windowsservercore

 RUN powershell (new-object 
System.Net.WebClient).Downloadfile('http://javadl.oracle.com/webapps/download/AutoDL?

  BundleId=210185', 'C:\jre-8u91-windows-x64.exe')
 RUN powershell start-process -filepath C:\jre-8u91-windows-x64.exe -passthru -wait -argumentlist 

"/s,INSTALLDIR=c:\Java\jre1.8.0_91,/L,install64.log"
 RUN del C:\jre-8u91-windows-x64.exe
 RUN set JAVA_HOME=""c:\\Java\\jre1.8.0_91\\"
 RUN set PATH=%PATH%,%JAVA_HOME%\bin
 CMD [ "c:\\Java\\jre1.8.0_91\\bin\\java.exe", "-version"]

but still i get shown the below error:

'javac' is not recognized as an internal or external command,
operable program or batch file.

Am i missing out on something. Any advise on this would be helpful.

IIRC if you are compiling a Java program then you need the JDK, not just the JRE.

Even tried with JDK still no go

FROM microsoft/windowsservercore:latest

ENV JAVA_PKG=jdk1.7.0_80/ \
    JAVA_HOME=C:\\jdk1.7.0_80

RUN setx /M PATH %PATH%;%JAVA_HOME%\bin

Please find the below response for the build

      ` PS C:\Users\Administrator> docker build --rm -t testing:java c:\testing
    Sending build context to Docker daemon 3.072 kB

 ---> 4f222178c921
Step 2/4 : ENV JAVA_PKG jdk1.7.0_80/ JAVA_HOME C:\\jdk1.7.0_80
 ---> Running in deefd35c044f
 ---> 33b3adfe3920
Removing intermediate container deefd35c044f
Step 3/4 : ADD $JAVA_PKG /
GetFileAttributesEx jdk1.7.0_80\: The system cannot find the file specified.
PS C:\Users\Administrator> docker build --rm -t testing:java c:\testing
Sending build context to Docker daemon 3.072 kB
Step 1/4 : FROM microsoft/windowsservercore:latest
 ---> 4f222178c921
Step 2/4 : ENV JAVA_PKG jdk1.7.0_80/ JAVA_HOME C:\\jdk1.7.0_80
 ---> Using cache
 ---> 33b3adfe3920
Step 3/4 : ADD JAVA_PKG
ADD requires at least two arguments
PS C:\Users\Administrator> docker build --rm -t testing:java c:\testing
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM microsoft/windowsservercore:latest
 ---> 4f222178c921
Step 2/3 : ENV JAVA_PKG jdk1.7.0_80/ JAVA_HOME C:\\jdk1.7.0_80
 ---> Using cache
 ---> 33b3adfe3920
Step 3/3 : RUN setx /M PATH %PATH%;%JAVA_HOME%\bin
 ---> Running in 3c1a328e6170

SUCCESS: Specified value was saved.
 ---> b6963b23e14b
Removing intermediate container 3c1a328e6170
Successfully built b6963b23e14b`

I get shown the same error. Any advise on this would be helpful. If possible kindly help me with the same Dockerfile to run the java Program

I managed to work javac but now it says File not found error. Please find my docker file

FROM microsoft/windowsservercore

# $ProgressPreference: https://github.com/PowerShell/PowerShell/issues/2138#issuecomment-251261324
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ENV JAVA_HOME C:\\ojdkbuild
RUN $newPath = ('{0}\bin;{1}' -f $env:JAVA_HOME, $env:PATH); \
	Write-Host ('Updating PATH: {0}' -f $newPath); \
# Nano Server does not have "[Environment]::SetEnvironmentVariable()"
	setx /M PATH $newPath;

# https://github.com/ojdkbuild/ojdkbuild/releases
ENV JAVA_VERSION 8u131
ENV JAVA_OJDKBUILD_VERSION 1.8.0.131-1
ENV JAVA_OJDKBUILD_ZIP java-1.8.0-openjdk-1.8.0.131-1.b11.ojdkbuild.windows.x86_64.zip
ENV JAVA_OJDKBUILD_SHA256 7e7384636054001499ba96d55c90fc39cbb0441281254a1e9ac8510b527a7a46

RUN $url = ('https://github.com/ojdkbuild/ojdkbuild/releases/download/{0}/{1}' -f $env:JAVA_OJDKBUILD_VERSION, $env:JAVA_OJDKBUILD_ZIP); \
	Write-Host ('Downloading {0} ...' -f $url); \
	Invoke-WebRequest -Uri $url -OutFile 'ojdkbuild.zip'; \
	Write-Host ('Verifying sha256 ({0}) ...' -f $env:JAVA_OJDKBUILD_SHA256); \
	if ((Get-FileHash ojdkbuild.zip -Algorithm sha256).Hash -ne $env:JAVA_OJDKBUILD_SHA256) { \
		Write-Host 'FAILED!'; \
		exit 1; \
	}; \
	\
	Write-Host 'Expanding ...'; \
	Expand-Archive ojdkbuild.zip -DestinationPath C:\; \
	\
	Write-Host 'Renaming ...'; \
	Move-Item \
		-Path ('C:\{0}' -f ($env:JAVA_OJDKBUILD_ZIP -Replace '.zip$', '')) \
		-Destination $env:JAVA_HOME \
	; \
	\
	Write-Host 'Verifying install ...'; \
	Write-Host '  java -version'; java -version; \
	Write-Host '  javac -version'; javac -version; \
	\
	Write-Host 'Removing ...'; \
	Remove-Item ojdkbuild.zip -Force; \
	\
	Write-Host 'Complete.';

when i try to compile the java program i get shown the below error

PS C:\java-windows-docker> docker build --rm --no-cache -t java-windows-docker C:\java-windows-docker
Sending build context to Docker daemon 3.584 kB
Step 1/5 : FROM testing:java
 ---> b48aa86d9f0a
Step 2/5 : WORKDIR C:\\java-windows-docker
 ---> febd44c4f9e0
Removing intermediate container 0d89dcf3326c
Step 3/5 : RUN javac Hello.java
 ---> Running in 675cfffa4564
javac: file not found: Hello.java
Usage: javac <options> <source files>
use -help for a list of possible options
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; javac Hello.
java' returned a non-zero code: 1