I can successsfully compile any java classes using this sample code:
static String testFileName = "OOJavaBootcampTest";
System.out.println("Compiling student test source file");
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
+ "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + " "
+ "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
+ testFileName + ".java\" >> \"" + ""
+ "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1";
System.out.println("Compile student source command: "
+ compileCommand);
try {
Runtime.getRuntime().exec(compileCommand).
waitFor(5000, TimeUnit.MILLISECONDS);
} catch (IOException | InterruptedException ex) {
}
This works fine but compiling the Junit test class with this same code but jUnit classpath added:
final static String jUnitFileName = "junit-4.11.jar";
...
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
+ "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + " "
+ "\"C:\\submissions\\SOFT222\\" + jUnitFileName
+ "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
+ testFileName + ".java\" >> \"" + ""
+ "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1";
The test.java file doesnt seem to copy to the specified directory in -d. This is error in the compilerOutput file:
javac: invalid flag: C:\submissions\SOFT222\junit-4.11.jar
Usage: javac <options> <source files>
use -help for a list of possible options
Junit is definitely in the specified location.
Classpath entries need to be separated by a ; (on Windows)
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
+ "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + ";"
+ "\"C:\\submissions\\SOFT222\\" + jUnitFileName
+ "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
+ testFileName + ".java\" >> \"" + ""
+ "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1"
Related
I'm using Runtime.getRuntime.exec(String) to cut some songs with ffmpeg.
But when my song has a name with a blankspace it doesn't work ...
So before I cut the song, I want to replace every blank space of my songs by "\ ".
I did that :
String in = directory+songs.get(i);
String out = directory+"trimed_"+songs.get(i);
in.replaceAll(" "," \\ ");
out.replaceAll(" ", "\\ ");
String str = "ffmpeg -t 1 -i "+in+" -vcodec copy "+out;
Runtime.getRuntime().exec(str);
But it doesn't replace anything at all when I print str, am I missing something ?
Update : I tried every ideas given bellow and I didn't find a way to fix the problem. Hence, I replaced the blankspaces by "_" and it's working great.
Try
String in = directory+songs.get(i);
String out = directory+"trimed_"+songs.get(i);
/* in = in.replaceAll("\\s","\\\\ ");
out = out.replaceAll("\\s","\\\\ ");
*/
in = "\"" + in + "\"";
out = "\"" + out + "\"";
String str = "ffmpeg -t 1 -i " + in + " -vcodec copy " + out;
Runtime.getRuntime().exec(str);
System.out.println("Command executed " + str);
Note: I tested this code myself its working fine.
If it still not working then execute the command manually by copying the str from log and trace the error
String s = "cmd /c " + GeneralMethods.getFilesPath()
+ "mysql-5.6.26-winx64/bin/mysqldump -u root -password database_name>" + GeneralMethods.getFilesPath()
+ "backup/backup.sql";
I dont want static code
I want to choose dynamic path on java.
If you are running the code from main method, pass the variables as program arguments. Read these arguments in the method and create the path.
For example
class Test {
public static void main(String[] args) {
String s = "cmd /c " + GeneralMethods.getFilesPath() + args[0] + "-u root -password database_name>" + GeneralMethods.getFilesPath() + args[1];
}
}
For any application VM arguments can be used.
String s = "cmd /c " + GeneralMethods.getFilesPath() + System.getProperty("sysProp1") + "-u root -password database_name>" + GeneralMethods.getFilesPath() + System.getProperty("sysProp2");
These variables sysProp1 or sysProp2 can be set using -DsysProp1=value1 -DsysProp2=value2 when starting the server / application.
Or if they are required at multiple places in the application
Create a class to hold these values
public class HolderClass {
public static String value1 = null;
public static String value2 = null;
}
Now, in the method where these values are available (in main method for example), set the values,
HolderClass.value1 = "value received"
HolderClass.value2 = "Other value received"
At the point where this value is required, use as
String s = "cmd /c " + GeneralMethods.getFilesPath() + HolderClass.value1 + "-u root -password database_name>" + GeneralMethods.getFilesPath() + HolderClass.value2;
Okay, I have a problem, I'm creating a launcher for Minecraft. My problem: I get this error messege, when I try launch Minecraft: Error: Could not find or load main class net.minecraft.client.main.Main
My code, I writted this, but not works:
The Detail of code:
Logger as module, my program using this to write information to console.
JavaPaht as string, the Java path is stored in this.
GameLibraries as string too, the libraries stored in this.
MinMemAlloc as string, the minimal allocated memory for java.
MaxMemAlloc as string, the maximal allocated memory for java.
Root as string, this is the root directory of Minecraft.
Here is the full sub to launch game:
Private Sub LaunchGame()
If Not File.Exists(Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + ".jar") Then
MsgBox("File not found: " + SelectedGameVersion + ".jar")
Else
Logger.Write("Launching Game...")
Logger.SetScrollDown()
Dim Gamelibraries As String = Nothing
For i = 0 To FileList.Count - 1
Gamelibraries += FileList.Item(i) + ";" +
Environment.NewLine()
Next
Logger.WriteWithJumpDown("Libraries loaded: " & Gamelibraries.ToString())
Logger.SetScrollDown()
Logger.Write("Building Process...")
Logger.Write("Received data: ")
Logger.SetScrollDown()
Dim p As New Process()
p.StartInfo.FileName = JavaPath
p.StartInfo.Arguments = " -Xms" + MinMemAlloc + "M -Xmx" + MaxMemAlloc + "M " +
"-Djava.library.path=" + Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + "-natives -cp " +
Gamelibraries.ToString() +
Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + ".jar " + mainClass +
" --username=" + UserID +
" --version " + SelectedGameVersion +
" --gameDir " + Root +
" --assetsDir " + Root + "\assets" +
" --assetIndex " + assets +
" --accessToken null" +
" --userProperties {}" +
" --userType mojang" +
" --uuid (Default)"
p.StartInfo.WorkingDirectory = Root
p.StartInfo.CreateNoWindow = False
p.StartInfo.UseShellExecute = False
p.EnableRaisingEvents = True
Application.DoEvents()
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardOutput = True
AddHandler p.ErrorDataReceived, AddressOf p_OutputDataReceived
AddHandler p.OutputDataReceived, AddressOf p_OutputDataReceived
p.Start()
p.BeginErrorReadLine()
p.BeginOutputReadLine()
Logger.SetScrollDown()
Button1.Text = "Play"
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
End If
End Sub
And my program writing the output into my console, so I write here the output:
[14:34:41 INFO ] Libraries loaded: C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\java3d\vecmath\1.5.2\vecmath-1.5.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\sf\trove4j\trove4j\3.0.3\trove4j-3.0.3.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\ibm\icu\icu4j-core-mojang\51.2\icu4j-core-mojang-51.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\sf\jopt-simple\jopt-simple\4.6\jopt-simple-4.6.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\io\netty\netty-all\4.0.15.Final\netty-all-4.0.15.Final.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\google\guava\guava\17.0\guava-17.0.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\commons-io\commons-io\2.4\commons-io-2.4.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\mojang\authlib\1.5.16\authlib-1.5.16.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\mojang\realms\1.5\realms-1.5.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\commons\commons-compress\1.8.1\commons-compress-1.8.1.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\httpcomponents\httpclient\4.3.3\httpclient-4.3.3.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\httpcomponents\httpcore\4.3.2\httpcore-4.3.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\logging\log4j\log4j-api\2.0-beta9\log4j-api-2.0-beta9.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\logging\log4j\log4j-core\2.0-beta9\log4j-core-2.0-beta9.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\lwjgl\lwjgl\lwjgl\2.9.1\lwjgl-2.9.1.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.1\lwjgl_util-2.9.1.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\lwjgl\lwjgl\lwjgl-platform\2.9.1\lwjgl-platform-2.9.1-natives-windows.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\java\jinput\jinput-platform\2.0.5\jinput-platform-2.0.5-natives-windows.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\tv\twitch\twitch\6.5\twitch-6.5.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\tv\twitch\twitch-platform\6.5\twitch-platform-6.5-natives-windows-64.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\tv\twitch\twitch-external-platform\4.5\twitch-external-platform-4.5-natives-windows-64.jar;
[14:34:41 INFO ] Building Process...
[14:34:41 INFO ] Received data:
[14:34:41 INFO ]
[14:34:41 INFO ] Error: Could not find or load main class net.minecraft.client.main.Main
[14:34:41 INFO ]
Thanks for help!
You need to have the main class on the path after the jars are included "net.minecraft.client.main.Main" unless mainClass means that already, then I don't know.
I tried to run a jar from my code with the runtime command.The jar connects to another host through RMI.It throughs an exception and exits.
But when I open the jar from cmd it works fine.
The jar gets 5 arguments.
Code :
String runCommand = "java -jar WhiteboardStudent.jar "
+ "192.168.0.3" + " "
+ "2000" + " "
+ "2001" + " "
+ "test" + " 0" ;
final Process proc = Runtime
.getRuntime()
.exec(runCommand,null,new File("util/"));
Command Prompt :
java -jar WhiteboardStudent.jar 192.168.0.3 2000 2001 test 0
The code where the exception is thrown is here :
Object proxy = registry.lookup("RWD");
Whiteboard whiteboard = (Whiteboard)proxy;
I'm just stumped.any ideas?
The function that throws the exception :
Registry registry = LocateRegistry.getRegistry(
hostname, Integer.parseInt(whiteboardPort)
);
String serviceName = "RemoteWhiteboard" + sessionID;
Object proxy = registry.lookup(serviceName);
Whiteboard whiteboard = (Whiteboard)proxy;
int userid = whiteboard.userRegistry(userName, "");
Main mainFrame = new Main(sessions, sessionID,
whiteboard,userid,"");
My suggestion is try the absolute path in your code:
String runCommand = "java -jar /c/users/xxx/WhiteboardStudent.jar "
+ "192.168.0.3" + " "
+ "2000" + " "
+ "2001" + " "
+ "test" + " 0" ;
I currently have this code:
private void compile(){
List<File> files = getListOfJavaFiles();
//JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
//compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjectsFromFiles(files);
List<String> optionList = new ArrayList<String>();
// set compiler's classpath to be same as the runtime's
optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));
//need to add options here.
compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1).call();
//compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
// fileManager.close();
}
But I am stuck now trying to make this actually run the files which have been compiled.
I see no output from this in the console, however in the Main.java file which I have compiled successfully (I can see the .class files), I have put "System.out.println("Main class is running");, so I would expect to see this when I run the application.
You can create an URLClassLoader to load your newly compiled classes, or you can have a look at a library I wrote which will compile in memory and load into the current class loader by default.
http://vanillajava.blogspot.co.uk/2010/11/more-uses-for-dynamic-code-in-java.html
If you have generated code, it will save the file to a source directory when debugging so you can step into the generated code (otherwise it does everything in memory)
You can only load a class once this way so if you need to load many versions I suggest you implement an interface and change the name of the class each time.
// this writes the file to disk only when debugging is enabled.
CachedCompiler cc = CompilerUtils.DEBUGGING ?
new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
CompilerUtils.CACHED_COMPILER;
String text = "generated test " + new Date();
Class fooBarTeeClass = cc.loadFromJava("eg.FooBarTee", "package eg;\n" +
'\n' +
"import eg.components.BarImpl;\n" +
"import eg.components.TeeImpl;\n" +
"import eg.components.Foo;\n" +
'\n' +
"public class FooBarTee{\n" +
" public final String name;\n" +
" public final TeeImpl tee;\n" +
" public final BarImpl bar;\n" +
" public final BarImpl copy;\n" +
" public final Foo foo;\n" +
'\n' +
" public FooBarTee(String name) {\n" +
" // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
" System.out.println(\"" + text + "\");\n" +
" this.name = name;\n" +
'\n' +
" tee = new TeeImpl(\"test\");\n" +
'\n' +
" bar = new BarImpl(tee, 55);\n" +
'\n' +
" copy = new BarImpl(tee, 555);\n" +
'\n' +
" // you should see the current date here after synchronisation.\n" +
" foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
" }\n" +
'\n' +
" public void start() {\n" +
" }\n" +
'\n' +
" public void stop() {\n" +
" }\n" +
'\n' +
" public void close() {\n" +
" stop();\n" +
'\n' +
" }\n" +
"}\n");
// add a debug break point here and step into this method.
FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
Foo foo = fooBarTee.foo;
assertNotNull(foo);
assertEquals(text, foo.s);