0

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 advise...

4
  • It might be best to use a scripting language (bash or perl or ...) for this task. Commented Aug 3, 2010 at 19:02
  • 1
    I want this to be done in Java only... Commented Aug 3, 2010 at 19:02
  • 1
    I'm sorry, but you're repeating that you're an absolute newbie with Java and you still want to write a Java program to compile and run other Java programs? Wouldn't it be better to start with writing "Hello Worlds" to get to know the language and then later on learn how to execute commandline tools and apply that to how you compiled and ran your own programs? Commented Aug 3, 2010 at 19:29
  • Is there a use case for this? Why do you think you need such a thing? Commented Aug 3, 2010 at 20:11

9 Answers 9

4

Try using Ant

The documentation should answer your questions.

The HelloWorld app can be found here

Sign up to request clarification or add additional context in comments.

2 Comments

How to use Ant... Am a beginner in Java.. I tried downloading Ant but it didnt work for me.
@Anad I suggest you to create a question "How to run ant for absolute 0" or something like that, it's very easy to build and run a set of files using ant
3

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 

3 Comments

Should work in general. I'm not sure about your requirements & scenario. Give it a shot it's pretty simple to try.
It does assume the other Java files have already been compiled.
That's right. However, if you have the source code for all programs then you can compile them when you compile your program using Ant or a simple batch file (if you don't know how to use ant).
2

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.

1 Comment

1

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.

Comments

1

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

Comments

0

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

Pass the .java filename(s) to compile.

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

Pass the class name to execute.

2 Comments

.exe? All the world is Windows now?
@asveikau It was an example. I almost added a note that the exact filename and path would be necessary but figured most would gather that on their own. Apparently, I was wrong.
0

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

Comments

0

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

Comments

0

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 :)).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.