简体   繁体   English

Java中的自动编译,自动运行,自动比较结果

[英]auto-compile, auto-run, auto-compare result in 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. 亲爱的所有人,我是Java的新手,现在我正尝试在Java下开发应用程序来执行以下操作:假设:存在一个包含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. 我们假设该文件包含一个主类(以及它的main方法和几个其他方法)和一些内部类,这些类将在main类运行时使用。

processes: 进程:

  1. the application which i want to develop (call it as ASIN), will try to compile the source code we have assumed above. 我要开发的应用程序(称为ASIN)将尝试编译上面假设的源代码。
  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). 我希望ASIN将分配一些内存空间32 Mb(例如),并给出最大运行时间2秒(例如)。
  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. 在分配了内存和运行的最大时间限制之后,ASIN将尝试使用给定的最大时间使用分配的资源(内存32 Mb)来运行Java字节码。 Then ASIN will record/capture result. 然后,ASIN将记录/捕获结果。 if the running byte code is using more than allocated memory, or exceeding the given time limit, ASIN will force to stop the execution. 如果正在运行的字节码使用的内存超过了分配的内存,或者超过了给定的时间限制,则ASIN将强制停止执行。 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? Java可以做上面的事情吗? any suggestion what should i do? 有什么建议我该怎么办?

regards, 问候,

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. 可能存在为此的开源系统,所以如果您还没有Google,请先在Google上搜索它。 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 eg -Xmx32m when starting the program with the java command. 如果您必须/想要自己做一个,我不确定Java是否可以限制您花费在程序上的时间(但是,您当然可以在等待一定时间后直接终止该程序),但是使用java命令启动程序时,可以使用-Xmx32m指定Java程序的内存限制。 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. 如果在UNIX系统上,则最简单的解决方案可能是使用ulimit来限制内存使用量和运行时间。

In either case, I think you'll have to use Runtime.exec() to launch both the compiler and the program. 无论哪种情况,我都认为您必须使用Runtime.exec()来启动编译器和程序。 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 eg 这也可以让您将输入输入到进程中,并从进程中读取输出(尽管如果您使用以下命令启动程序,也可以执行I / O重定向到文件或从文件重定向)。

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 ; 至于比较:除非您需要完全匹配(即使在空格中),否则最简单的解决方案可能是将输出重定向到文件并使用-b-w以及--ignore-blank-lines调用diff if the return code is 0, the output is equal to the solution (except for whitespace differences). 如果返回码为0,则输出等于解决方案(空格差除外)。 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 ; 另一方面,如果您需要交互式评分(该程序应该与评分器“对话”),或者如果输出可能以不同的方式形成,从而没有唯一的正确答案,则不能使用diff the grader must read the process' output stream instead. 评分员必须改为读取流程的输出流。

I'd do this in shell script rather than java. 我会在Shell脚本而不是Java中执行此操作。 Would that be an option? 这是一个选择吗?

You can do the memory and time limiting using ulimit. 您可以使用ulimit进行内存和时间限制。 If you write a small shell script that does this: 如果您编写一个执行此操作的小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

Then when you run it, it will give itself memory and time limits, and launch java as a subprocess. 然后,当您运行它时,它将给自己分配内存和时间限制,并将java作为子进程启动。 If the program completes in time (note that the limit is on CPU time, not real time), it will finish with exit status 0; 如果程序及时完成(请注意,限制是对CPU时间的限制,而不是实时的),则它将以退出状态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:

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. 当然,从脚本编译代码也很容易-只需运行javac。 Remember to check the exit status to see if the code compiled correctly. 切记检查退出状态以查看代码是否正确编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Windows 启动时自动运行 - Auto-run on Windows startup 自动编译Bash脚本无法正常工作 - Auto-Compile Bash Script not working Eclipse自动编译仅创建软件包,而不创建类文件 - eclipse auto-compile only creates packages- not class files 每当上下文加载时如何自动运行Java Web应用程序中的servlet /动作(tomcat启动) - how to auto-run a servlet/action in a java web application whenever the context loads (tomcat starts) 无论如何,在程序启动时是否可以在Universal Gcode Sender中自动运行文件,而不必上载文件? - Is there anyway to auto-run a file in Universal Gcode Sender on the program's startup instead of having to upload a file? 如何在 CD 后使用 Azure 自动运行 maven 项目并显示正确的网页 - How to auto-run maven project with Azure after CD and display correct webpage 在netbeans普通java项目中自动编译SCSS - Auto compile SCSS in netbeans plain java project 在JAVA中针对不同客户自动编译较少的文件 - Auto Compile Less in JAVA for different customer Less files 如何实现像Java Servlet,PHP CMS模板这样的自动编译? - How to implement auto compile like java Servlet , PHP CMS template? MySql Java自动增量 - MySql Java auto increment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM