Can a .bat script write a a java file and compile it?
In this README I gi开发者_运维百科ve instructions for a quick CL for testing the released tool. I think it would be much better if I provided a .bat and unix script which executed the commands in one click/command. At the same time, unlike a compiled program, it's transparent and users can open the script with the editor and inspect the commands executed.
Can I in a bat save a file? This is what I'd like it to execute.
$ vim Test.java (windows: notepad Test.java)
class T {
private static void p(int i, Double d, String... s){}
}
public class Test{
@com.dp4j.InjectReflection public void t() {
T.p(1,new Double(2),"hello", "reflection");
}
}
$ ls Test.class T.class (windows: dir Test.class T.class)
ls: Test.class: No such file or directory ls: T.class: No such file or directory
$ javac -cp dp4j-1.0-jar-with-dependencies.jar Test.java
$ ls Test.class T.class (windows: dir Test.class T.class)
ls Test.class T.class
Yes.
You can put each line in an echo
command piped to the file:
echo class T { > MyFile.java
echo private static void p(int i, Double d, String... s){} >> MyFile.java
echo } >> MyFile.java
echo ... >> MyFile.java
>
creates a file; >>
appends to it.
You can then compile it normally.
Um, perhaps this?
// 2>NUL&GOTO :START
//Just ignore the above line, it's for the batch script.
class T {
private static void p(int i, Double d, String... s){}
}
public class Test{
@com.dp4j.InjectReflection public void t() {
T.p(1,new Double(2),"hello", "reflection");
}
}
/*We start the batch script here.
:START
@CLS&ECHO OFF
START NOTEPAD %0
IF EXIST Test.class GOTO :EXISTS
IF EXIST T.class GOTO :EXISTS
JAVAC -cp dp4j-1.0-jar-with-dependencies.jar %0
GOTO :END
:EXISTS
ECHO There is a preexisting class file. Aborting.
:END
REM We end the batch script here.*/
By the way, here the batch script and java source are the same file.
This is a unix exectable script that does what I want. I don't know if it works with cygwin on windows:
#!/bin/sh
v=1.1
test_file="Test10.java"
jar_file="dp4j-$v-jar-with-dependencies.jar"
cmd="curl -O --fail -L http://downloads.sourceforge.net/project/dp4j/$v/$jar_file"
echo $cmd
$cmd
echo
# Start
cat > $test_file << __EOF__
class T10 {
private static void p(int i, Double d, String... s){}
}
public class Test10{
@com.dp4j.InjectReflection
public void t() {
T10.p(1,new Double(2),"hello", "reflection");
}
}
__EOF__
cmd="cat $test_file"
echo $cmd
$cmd
echo
cmd="javac -Averbose=true -cp $jar_file $test_file"
echo $cmd
$cmd
echo
echo "TEST PASSED: $test_file was compiled with Reflection Injected."
echo "When JUnit/TestNG.jar is in the classpath you may use @Test in lieu of @InjectReflection."
echo "Javadoc, sources, and other artifacts maybe downloaded from http://repo2.maven.org/maven2/com/dp4j/dp4j/"$v"/"
Here's one line command to download it and execute it:
wget http://sourceforge.net/projects/dp4j/files/1.2/TESTDRIVE ; chmod +x TESTDRIVE ; ./TESTDRIVE
精彩评论