2

I have a simple program in java which is run inside docker container.

I used Eclipse On windows - But I created the linux containers (with windwos docker desktop)

I used Powershell to start container The image is successfully created

I saw this thread but it didnt help me c++ program with docker. I have an error using the solution in that answer.

PS E:\Java\Test> docker run -v E:\Java\Test:\tt img \tt\in.txt

docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "\\tt\\in.txt": executable file not found in $PATH": unknown.

Dockerfile

FROM java:8
COPY  hello.jar app.jar
CMD ["java", "-jar", "app.jar", "pkg.hello"]



public static  void main(String[] args) throws Exception {
    String fileName ="E:\\Java\\Test\\in.txt";
    List<String> lines = Collections.emptyList();
    lines =   Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
    Iterator<String> itr = lines.iterator(); 
    while (itr.hasNext()) 
      System.out.println(itr.next()); 
    }

Any ideas, thanks

Azzurro94
  • 479
  • 1
  • 7
  • 19

2 Answers2

2

New Docker file

FROM java:8

COPY  hello.jar app.jar

ENTRYPOINT ["java", "-jar", "app.jar", "pkg.hello"]

Ok after lots of trial and error I found one solution, there might be more than one. I want to explain a bit for those who are not very expert like me,

As we know docker makes a something like a mini OS inside the container, I mean the required libraries and files + the application we developed. I tried to run and see what is inside my directory when running in docker. by running on eclipse on windows it shows the regular path, E:\Java\.... . But when I ran it on docker it shows only "/". and then I tried to see the contents. on windows the files and folders inside the current Dir but on docker it shows some folders same as LINUX root path folders, bin sbin mnt proc etc. So I realized that there is a mini linux. I knew this fact but never seen this result. So obviously the in.txt is not there in docker file system. now we see the docker features like -v. it allows us to copy/paste one directory from host, which is windows, to docker container file system. After running this command I saw that a new folder called data is added to docker folders

docker run -v ${pwd}:/data img

the whole contents of pwd (current directory on Win) is copied to data directory, along with in.txt. now my java program is supposed to be run inside the docker, So i need to change the hard-coded path.

String fileName ="data//in.txt"; 

definitely it does not work on windows because there is not \data\in.txt in my folders. I could do it by getting some arguments and skip the hard-code, no matter. there are plenty of ways. I added my codes to see the Dir status, you may want to try.

String d = System.getProperty("user.dir");
    File f = new File(d);
    File filesList[] = f.listFiles();
    for(File file : filesList) {
         System.out.println("File name: "+file.getName());
         System.out.println("File path: "+file.getAbsolutePath());
         if (file.getName().equals("data")==true)
         {
             File filesList2[] = file.listFiles();
             for(File file2 : filesList2) {
             System.out.println("File name: "+file2.getName());
             System.out.println("File path: "+file2.getAbsolutePath());
             }
             
         }
         System.out.println(" ");
      }
Azzurro94
  • 479
  • 1
  • 7
  • 19
0

I think in this case you will need to use ENTRYPOINT instead of CMD. In short:

With CMD when you run the container, the instruction next to container name will become the new CMD used (from here the error "\\tt\\in.txt": executable file not found in $PATH": unknown). On the other hand, with ENTRYPOINT the instruction you add before the container name will be added as an additional argument.

Check this article for a longer explanation about CMD and ENTRYPOINT.

salpreh
  • 190
  • 2
  • 6
  • no, running with Entrypoint gives me the error: fileNotFound exception" in the address. but the file exist there and program works without docker – Azzurro94 Aug 17 '20 at 01:00
  • At least now seems that your jar is executing. About the path I think @tgdavies is on the right track. Containers are Linux sistems, so when you are refering paths inside container should go with `/` separator. Try to change path in mapping and path argument: `docker run -v E:\Java\Test:/tt img /tt/in.txt` – salpreh Aug 17 '20 at 08:09