Is Runtime.getRuntime().Exec() environment diffrent from cmd env? - java

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" ;

Related

How to get dynamic path in java

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;

Replacing a test.class file with a test.java file using Junit

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"

`rtserver-id` turns to `rtserver - id` in java string

I have this code:
public void foo (){
String script =
"var aLocation = {};" +
"var aOffer = {};" +
"var aAdData = " +
"{ " +
"location: aLocation, " +
"offer: aOffer " +
" };" +
"var aClientEnv = " +
" { " +
" sessionid: \"\", " +
" cookie: \"\", " +
" rtserver-id: 1, " +
" lon: 34.847, " +
" lat: 32.123, " +
" venue: \"\", " +
" venue_context: \"\", " +
" source: \"\"," + // One of the following (string) values: ADS_PIN_INFO,
// ADS_0SPEED_INFO, ADS_LINE_SEARCH_INFO,
// ADS_ARROW_NEARBY_INFO, ADS_CATEGORY_AUTOCOMPLETE_INFO,
// ADS_HISTORY_LIST_INFO
// (this field is also called "channel")
" locale: \"\"" + // ISO639-1 language code (2-5 characters), supported formats:
" };" +
"W.setOffer(aAdData, aClientEnv);";
javascriptExecutor.executeScript(script);
}
I have two q:
when I debug and copy script value I see a member rtserver - id instead of rtserver-id
how can it be? the code throws an exception because of this.
Even if i remove this rtserver-id member (and there is not exception thrown)
I evaluate aLocation in this browser console and get "variable not defined". How can this be?
rtserver-id isn't a valid identifier - so if you want it as a field/property name, you need to quote it. You can see this in a Chrome Javascript console, with no need for any Java involved:
> var aClientEnv = { sessionId: "", rtserver-id: 1 };
Uncaught SyntaxError: Unexpected token -
> var aClientEnv = { sessionId: "", "rtserver-id": 1 };
undefined
> aClientEnv
Object {sessionId: "", rtserver-id: 1}
Basically I don't think anything's adding spaces - you've just got an invalid script. You can easily add the quotes in your Java code:
" \"rtserver-id\": 1, " +

vb.net how to launch Minecraft?

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.

Java vs. Net HTTP Client Performance

We call a webservice from our C# app which takes about 300ms using WCF (BasicHttpBinding). We noticed that the same SOAP call does only take about 30ms when sending it from SOAP UI.
Now we also implemented a test accessing the webservice via a basic WebClient in order to make sure that the DeSer-part of the WCf is not the reason for this additional delay. When using the WebClient class the call takes about 300ms as well.
Any ideas on why Java compared to C# is about 10x faster in this regard? Is there some kind of tweaking possible on the .NET side of things?
private void Button_Click(object sender, RoutedEventArgs e)
{
executeTest(() =>
{
var resultObj = client.getNextSeqNr(new WcfClient()
{
domain = "?",
hostname = "?",
ipaddress = "?",
loginVersion = "?",
processId = "?",
program = "?",
userId = "?",
userIdPw = "?",
userName = "?"
}, "?", "?");
});
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
executeTest(()=>
{
webClient.Proxy = null;
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
webClient.Headers.Add("Content-Type", "application/xml");
webClient.Encoding = Encoding.UTF8;
var data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"SomeNamespace\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <ser:getNextSeqNr>" +
" <!--Optional:-->" +
" <clientInfo>" +
" <!--Optional:-->" +
" <domain>?</domain>" +
" <!--Optional:-->" +
" <hostname>?</hostname>" +
" <!--Optional:-->" +
" <ipaddress>?</ipaddress>" +
" <!--Optional:-->" +
" <loginVersion>?</loginVersion>" +
" <!--Optional:-->" +
" <processId>?</processId>" +
" <!--Optional:-->" +
" <program>?</program>" +
" <!--Optional:-->" +
" <userId>*</userId>" +
" <!--Optional:-->" +
" <userIdPw>?</userIdPw>" +
" <!--Optional:-->" +
" <userName>?</userName>" +
" </clientInfo>" +
" <!--Optional:-->" +
" <name>?</name>" +
" <!--Optional:-->" +
" <schema>?</schema>" +
" </ser:getNextSeqNr>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
string result = webClient.UploadString("http://server:8080/service", "POST", data);
});
}
Am I missing something here? Any idea would be helpful... ;-)
Kind regards,
Sebastian
I just found the reason for this.
It's the 100-Expect Continue HTTP Header and the corresponding implementation in .NET. The .NET client wait 350ms as default on the server. This causes the delays. Java seems to have other default values here...
Just add the following line of code very early in your code:
System.Net.ServicePointManager.Expect100Continue = false;
Cheers!

Categories

Resources