Java自动编译、自动运行、自动比较结果

发布于 2024-10-22 01:13:23 字数 473 浏览 5 评论 0原文

各位, 我是 Java 新手,现在我正在尝试在 Java 下开发一个应用程序来执行以下操作: 假设: 有一个文件包含Java源代码。 我们假设该文件包含一个主类(它是带有几个附加方法的主方法)和一些将在主类运行时使用的内部类。

流程:

  1. 我想要开发的应用程序(将其称为 ASIN)将尝试编译我们上面假设的源代码。
  2. 编译阶段完成后。我希望 ASIN 分配一些内存空间 32 Mb(例如),并给出最大运行时间 2 秒(例如)。
  3. 分配内存和最大运行时间限制后,ASIN 将尝试使用分配的资源(内存 32 Mb)在给定的最大时间下运行 java 字节代码。然后 ASIN 将记录/捕获结果。如果正在运行的字节码使用超过分配的内存,或超过给定的时间限制,ASIN 将强制停止执行。因此,正常情况是执行在最大时间限制之前完成并且使用的内存少于分配的内存。

java可以做上面的事情吗? 有什么建议我应该做什么?

问候,

dear all,
i am new in Java and at now i am trying to develop an application under Java to do such these things:
assumption:
there is a file contains source code in Java.
let's assume that the file contains a main class (and it's main method with several additional methods) and some inner classes which will be used during the main class running time.

processes:

  1. the application which i want to develop (call it as ASIN), will try to compile the source code we have assumed above.
  2. after compilation phase is finished. i want the ASIN will allocate some memory space 32 Mb (for instance), and give a maximum running time 2 seconds (for instance).
  3. after allocating the memory and the maximum time limit for running, ASIN will try to run the java byte code by using allocated resource (memory 32 Mb) with given maximum time. Then ASIN will record/capture result. if the running byte code is using more than allocated memory, or exceeding the given time limit, ASIN will force to stop the execution. so, the normal case is when the execution finished before the maximum time limit and use less than allocated memory.

can java do thing above?
any suggestion what should i do?

regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-10-29 01:13:23

这应该是编程竞赛或大学课程练习的评分系统吗?可能存在这样的开源系统,所以如果你还没有的话,请先用谷歌搜索一下。如果您必须/想要自己制作一个,我不确定Java是否允许您限制程序所花费的时间(当然,您可以在等待一定时间后简单地终止程序),但是当使用 java 命令启动程序时,可以使用例如 -Xmx32m 指定 java 程序的内存限制。如果这是在 UNIX 系统上,最简单的解决方案可能是使用 ulimit 来限制内存使用和运行时间。

无论哪种情况,我认为您都必须使用 Runtime.exec() 启动编译器和程序。这还可以让您将输入输入到进程中并读取进程的输出(尽管如果您使用例如启动程序,也可以对文件执行 I/O 重定向

Runtime.getRuntime().exec("java -Xmx32m Program.class < input.txt > output.txt");

至于比较:除非您需要完全匹配(即使在空白中),最简单的解决方案可能是将输出重定向到文件并使用 -b-w 调用 diff,并且可能 --ignore-blank-lines; 如果返回码为 0,则输出等于解决方案(空白差异除外)。应该与评分者“对话”)或者如果输出可能以不同的方式形成,因此没有唯一的正确答案,则不能使用 diff;评分者必须阅读该过程'而是输出流。

Is this supposed to be a grading system for a programming contest or for exercises in a university course? There might exist open-source systems for that, so google it first if you haven't already. If you have to / want to make one yourself, I'm not sure if Java lets you limit the time spent by a program (but you could, of course, simply kill the program after waiting a certain amount of time), but the memory limit of a java program can be specified with e.g. -Xmx32m when starting the program with the java command. If this is on a unix system, the simplest solution will probably be to use ulimit to limit both the memory usage and the run time.

In either case, I think you'll have to use Runtime.exec() to launch both the compiler and the program. This will also let you feed input into the process and read the output from the process (although I/O redirection could also be performed to/from files if you launch the program with e.g.

Runtime.getRuntime().exec("java -Xmx32m Program.class < input.txt > output.txt");

As for the comparison: unless you require an exact match (even in whitespace), the simplest solution is probably to redirect the output to a file and invoke diff with either -b or -w, and possibly --ignore-blank-lines; if the return code is 0, the output is equal to the solution (except for whitespace differences). On the other hand, if you need interactive grading (the program is supposed to "converse" with the grader) or if the output may be formed in different ways such that there is no unique correct answer, you can't use diff; the grader must read the process' output stream instead.

听你说爱我 2024-10-29 01:13:23

我会在 shell 脚本中而不是 java 中执行此操作。这是一个选择吗?

您可以使用 ulimit 来限制内存和时间。如果您编写一个小型 shell 脚本来执行此操作:

set -e
ulimit -v 32768 # ulimit -v works in 1 kB blocks, so this is 32 MB
ulimit -t 2 # ulimit -t works in seconds
java ProgramUnderTest >output

那么当您运行它时,它将给自己设置内存和时间限制,并将 java 作为子进程启动。如果程序及时完成(请注意,限制是 CPU 时间,而不是实时),它将以退出状态 0 结束;否则,它将具有一些非零退出状态。

如果你想记录限制内实际使用了多少时间,请在 time 下运行 java

time java ProgramUnderTest >output

这将打印输出后所用的总时间。

当然,从脚本编译代码也很容易 - 只需运行 javac 即可。请记住检查退出状态以查看代码是否正确编译。

I'd do this in shell script rather than java. Would that be an option?

You can do the memory and time limiting using ulimit. If you write a small shell script that does this:

set -e
ulimit -v 32768 # ulimit -v works in 1 kB blocks, so this is 32 MB
ulimit -t 2 # ulimit -t works in seconds
java ProgramUnderTest >output

Then when you run it, it will give itself memory and time limits, and launch java as a subprocess. If the program completes in time (note that the limit is on CPU time, not real time), it will finish with exit status 0; otherwise, it will have some nonzero exit status.

If you want to record how much actual time was used within the limit, run java under time:

time java ProgramUnderTest >output

That will print the total amount of time used after the output.

Compiling the code from a script is also easy, of course - just run javac. Remember to check the exit status to see if the code compiled correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文