使用具有自己路径的 docker 编译一个 java 文件 [英] Compile a java file using docker with own path

查看:23
本文介绍了使用具有自己路径的 docker 编译一个 java 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我正在尝试使用 docker 编译一个 .java 文件.我阅读了 docker 网站上的文件,也阅读了这些链接:

docker 的网站

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

关于卷

以及我为 gcc 编译器提出的另一个问题

我理解 gcc 编译器的概念,因为它不会为编译创建任何额外的文件.但是 java 是这样的.如果我使用以下命令并编译名为 Main.java 的文件,它会在我的/home 目录中创建一个 Main.class 文件

sudo docker run --rm -v "$PWD":/usr/src/myapp -w/usr/src/myapp java:7 javac Main.java

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

从上述链接中学习后,我能够使用我自己的路径成功编译一个 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"

如果有任何错误,它会显示一个错误,但如果没有,它只会编译并且不给我任何输出,这是合理的,因为它创建了一个 Main.class 文件.

我的问题是我找不到那个 Main.class 文件.我不知道 docker 在哪里创建它,我对它的理解为零.请帮帮我.

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.

推荐答案

.class 文件将在容器内,在根目录下.

The .class file will be inside the container, under the root directory.

最好的计划是挂载整个源目录,并让 javac 将结果放到同一目录中,例如:

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"

这样,您应该将类​​文件写入mypathhere 目录.

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.

这篇关于使用具有自己路径的 docker 编译一个 java 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆