开发者

Java code for compiling and executing Java programs

开发者 https://www.devze.com 2023-01-10 14:50 出处:网络
I want to compile and execute a set of java programs one after the other. Please help me out by giving me some example program on how to carry out the task.

I want to compile and execute a set of java programs one after the other.

Please help me out by giving me some example program on how to carry out the task.

Please advis开发者_StackOverflowe...


Try using Ant

The documentation should answer your questions.

The HelloWorld app can be found here


Can't you call the main() method of the other programs inside your main method? Example:

ClassB { 
    public static void main(String[] args) { 
        System.out.println("ClassB main() Called"); 
    } 
} 

ClassA { 
    public static void main(String[] args) { 
        System.out.println("ClassA main() Called"); 
        ClassB.main(args); 
    } 
}

The output will be

ClassA main() Called
ClassB main() Called


You can also use the java compiler API programmatically. http://download-llnw.oracle.com/javase/6/docs/api/javax/tools/package-summary.html

The necessary tools jars are hidden away in the JDK directory.


When I had to do this I create a perl script that I ran. Within the script I used system calls to execute the java programs (one after the other). I was also able to change directory between the programs.


Use something like apache ant (http://ant.apache.org) or maven (http://maven.apache.org)


Runtime.getRuntime().exec("javac.exe");

Pass the .java filename(s) to compile.

Runtime.getRuntime().exec("java.exe");

Pass the class name to execute.


Why not using NetBeans or Eclipse? But once you got familiar with those tools, try compiling the sourcecode with 'javac' and executing the classes with 'java' from command line

See e.g. http://java.sun.com/developer/onlineTraining/tools/netbeans_part1/ or http://download.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.html


You could use ant

For instance, if you have two java files in the directory src

src/Hello.java
src/Hola.java

Using this build file will compile and run them:

<project default="compile">
        <!-- compile everything inside the "src" directory -->
    <target name="compile">
        <javac srcdir="src" destdir="classes" />
    </target>

        <!-- run the program named "Hello" followed by the program "Hola" -->
    <target name="run" depends="compile">
        <java classname="Hello" classpath="classes" />
        <java classname="Hello" classpath="classes" />
    </target>
</project>

Save the content in a build.xml and then type ant or ant run

$ ant run 
Buildfile: build.xml

compile:
  [javac] Compiling 2 source files to /Users/oscarryz/Oscar/code/languages/Java/useAnt/classes

run:
   [java] Hello
   [java] Hello

BUILD SUCCESSFUL
Total time: 0 seconds

To install ant in your system installing ant or this


So basically what you need is a build system, similar to the make and MAKEFILE combination for generic programming. A build system is a usually a program that parses the file which describes what actions the build system needs to make to produce executable. But as usual there can be significantly more things happening (you can execute the compiled files, generate documentation etc.).

So in the example above by OscarRyz, the ant project file defines the source directory where the files are and after compilation runs the files in that directory. The javac tag and java tag.

Good thing about ant is that it's also written in java so you really don't need to do anything else. And the documentation for ant has been pretty good, with examples, worth definitely reading.

I don't think it would be worthwhile, especially if you are beginner to start using weird Runtime constructs or tinkering with javac directly (unless you have significant amount of time available, which we usually don't :)).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号