Autoit Java argument in run command through $chosen variable - java

I made a combo:
$Combo1 = GUICtrlCreateCombo("Java Memory", 24, 872, 145, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "-Xmx1024M|-Xmx2048M|-Xmx3072M|-Xmx4096M")
Then I added something to read it:
$chosen = GUICtrlRead($Combo1)
Then I made a run command and put $chosen in it:
Run ("java -jar spigot-1.6.2-R0.1.jar " & $chosen, "E:\Spill\Alle spill\Minecraft\KnarCraft 2013")
When I don't choose an option in the drop down list, it starts. When I do, it comes a window that disappears instantly, but it shows all valid parameters so therefore something is wrong in the way it reads it. I think it has something to do with the - but I don't know how i should do it. I tried using a - and then the variable, but then it reads it as -$chosen instead of "-" + "choice in $chosen".

First off, the order of your java command line I believe is important, and so the -Xmx option should come after the "java" and before the "-jar" tokens.
Next, I wonder if you're trying to use too much memory. Have you considered testing this with smaller values?
For example:
$Combo1 = GUICtrlCreateCombo("Java Memory", 10, 10, 142, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "-Xmx100M|-Xmx200M|-Xmx400M|-Xmx800M|-Xmx1024M|-Xmx2048M|-Xmx3072M|-Xmx4096M")
Then see if any of the smaller numbers work and if the larger numbers break the program.
My test program:
AutoIt program, MyFoo.au3:
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
Example()
Func Example()
Local $msg
GUICreate("My GUI combo") ; will create a dialog box that when displayed is centered
$Combo1 = GUICtrlCreateCombo("Java Memory", 10, 10, 142, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "-Xmx100M|-Xmx200M|-Xmx400M|-Xmx800M|-Xmx1024M|-Xmx2048M|-Xmx3072M|-Xmx4096M")
GUISetState()
; Run the GUI until the dialog is closed
While 1
$msg = GUIGetMsg()
If $msg = $Combo1 Then
$chosen = GUICtrlRead($Combo1)
$runString1 = "java " & $chosen & " -jar MyFoo.jar"
$runString2 = "java -jar MyFoo.jar " & $chosen
ConsoleWrite($runString1 & #CRLF)
Run($runString1)
EndIf
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
EndFunc
Java test program, MyAutoItFoo.java. Of course this was jar'd first:
import javax.swing.JOptionPane;
public class MyAutoItFoo {
public static void main(String[] args) {
long heapSize = Runtime.getRuntime().totalMemory();
long heapMaxSize = Runtime.getRuntime().maxMemory();
String heapString = String.format("Heap Size = %H; Max Heap = %H",
heapSize, heapMaxSize);
System.out.println(heapString);
JOptionPane.showMessageDialog(null, heapString);
}
}

Related

vscode coderunner has trouble executing java code

I have the following structure:
java_projects > chapter9
usually i cd to a directory a level above java_projects, and do code java_projects to create a workspace directory from that location. Now, inside chapter 9, i have the folloiwng files:
Tv.java, TestTv.java
Inside TestTv.java i have the following code:
package chapter9;
public class TestTv {
public static void main(String[] args){
Tv tv = new Tv();
tv.turnOn();
tv.setChannel(30);
tv.setVolume(3);
System.out.println("Tv channel is: " + tv.channel);
// testing java date class
java.util.Date date = new java.util.Date();
System.out.println("time elapsed since jan 1, 1970 is: " + date.getTime() + "milliseconds");
System.out.println(date.toString());
}
}
when i run this via code-runner, i get the following error:
TestTv.java:5: error: cannot find symbol
Tv tv = new Tv();
^
symbol: class Tv
location: class TestTv
TestTv.java:5: error: cannot find symbol
Tv tv = new Tv();
^
symbol: class Tv
location: class TestTv
I'm not too sure why this is happening. Upon further investigating, i learned this has to do with compiling the Tv Class first before its being used in java program. Ok, so i looked into my settings.json inside vscode and changed it to:
"java": cd $dir && javac *.java && java $fileName"
This still gives me the same error.
So i cd'd out of the chapter9 directory and tried java chapter9/TestTv.java and it worked!!
How do i tell vscode's executor map to go back one level up and execute it?
Thanks
{
"code-runner.clearPreviousOutput": true,
"code-runner.ignoreSelection": true,
"code-runner.saveAllFilesBeforeRun": true,
"java.autobuild.enabled": true,
"code-runner.fileDirectoryAsCwd": false,
"java.configuration.updateBuildConfiguration": "automatic",
"code-runner.executorMap": {
"java": "cd $dir && javac *.java && java $fileName"
},
"code-runner.runInTerminal": true,
}
Attached is my tvclass
public class Tv {
// instance variables
int channel = 1; // default channel is 1
int volumeLevel = 1;
boolean on = false; // Tv is off
public Tv(){};
public void turnOn(){
on = true;
}
public void turnOff(){
on = false;
}
public void setChannel(int newChannel){
if (on && newChannel >= 1 && newChannel <= 120)
channel = newChannel;
}
public void setVolume(int newVolumeLevel){
if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7){
volumeLevel = newVolumeLevel;
}
}
public void channelUp(){
if (on && channel < 120)
channel++;
}
public void channelDown(){
if (on && channel > 1)
channel--;
}
public void volumeUp(){
if (on && volumeLevel < 7)
volumeLevel++;
}
public void volumeDown(){
if (on && volumeLevel > 1)
volumeLevel--;
}
Im looking for something like this?
"java": cd $dir && javac *.java && java (go back one directory up) $dir[my_folder_name]fileName"
so something like this...
"java": cd $dir && javac *.java && cd .. java chapter9/TestTv.java"
I don't know i can't make it any more clearer. I have a mac OSX, sierra, javaSE11. I don't think it has to do with the operating system. Its more of a compilation issue. going to the vscode's github doesn't help as they don't reply to any thing.
I am also not very familiar with the topic. I did some trial and error and managed it to work with the following:
"code-runner.executorMap": {
"java": "cd $dir && javac *.java && cd .. && java \"$(basename $dir)/$fileNameWithoutExt\""
}
Notable parts/changes:
&& cd .. moves one directory up.
$(basename $dir) returns the most inner directory. chapter9 in your example, but if you change to another directory you don't have to change it.
$fileNameWithoutExt: The base name of the code file being run without its extension, as documented here under Configuration.
I'm less familiar with this topic, but based looking at this syntax in your problem/answer and seeing the "&&" syntax as a series of scripts, it looks like you need one more "&&" in the mix so the "cd .." moves up to the parent directory BEFORE calling the java-interpreter as the last thing.
so:
"java": cd $dir && javac *.java && cd .. && java chapter9.TestTv"
Also I'm less familiar with mac requirements, but I know some systems don't mind "cd.." and some only take "cd .." with the space, so be cautious as you type it in..

java.nio.ByteBuffer wrap method partly working with sbt run

I have an issue where I read a bytestream from a big file ~ (100MB) and after some integers I get the value 0 (but only with sbt run ). When I hit the play button on IntelliJ I get the value I expected > 0.
My guess was that the environment is somehow different. But I could not spot the difference.
// DemoApp.scala
import java.nio.{ByteBuffer, ByteOrder}
object DemoApp extends App {
val inputStream = getClass.getResourceAsStream("/HandRanks.dat")
val handRanks = new Array[Byte](inputStream.available)
inputStream.read(handRanks)
inputStream.close()
def evalCard(value: Int) = {
val offset = value * 4
println("value: " + value)
println("offset: " + offset)
ByteBuffer.wrap(handRanks, offset, handRanks.length - offset).order(ByteOrder.LITTLE_ENDIAN).getInt
}
val cards: List[Int] = List(51, 45, 14, 2, 12, 28, 46)
def eval(cards: List[Int]): Unit = {
var p = 53
cards.foreach(card => {
println("p = " + evalCard(p))
p = evalCard(p + card)
})
println("result p: " + p);
}
eval(cards)
}
The HandRanks.dat can be found here: (I put it inside a directory called resources)
https://github.com/Robert-Nickel/scala-texas-holdem/blob/master/src/main/resources/HandRanks.dat
build.sbt is:
name := "LoadInts"
version := "0.1"
scalaVersion := "2.13.4"
On my windows machine I use sbt 1.4.6 with Oracle Java 11
You will see that the evalCard call will work 4 times but after the fifth time the return value is 0. It should be higher than 0, which it is when using IntelliJ's play button.
You are not reading a whole content. This
val handRanks = new Array[Byte](inputStream.available)
allocates only as much as InputStream buffer and then you read the amount in buffer with
inputStream.read(handRanks)
Depending of defaults you will process different amount but they will never be 100MB of data. For that you would have to read data into some structure in the loop (bad idea) or process it in chunks (with iterators, stream, etc).
import scala.util.Using
// Using will close the resource whether error happens or not
Using(getClass.getResourceAsStream("/HandRanks.dat")) { inputStream =>
def readChunk(): Option[Array[Byte]] = {
// can be done better, but that's not the point here
val buffer = new Array[Byte](inputStream.available)
val bytesRead = inputStream.read(buffer)
if (bytesRead >= 0) Some(buffer.take(bytesRead))
else None
}
#tailrec def process(): Unit = {
readChunk() match {
case Some(chunk) =>
// do something
process()
case None =>
// nothing to do - EOF reached
}
}
process()
}

Java OpenCV cant copy from MatOfInt4 into MatOfInt4

I have an OpenCV 4.2.0 application using HoughLinesP to detect lines in an image that's working fine with C++ and Objective-C. Now I need to have the same functionality working for java on Android.  
I've been fighting with it for a couple of days now and boiled it down to the following issue.  I can't copy a value from one MatOfInt4 to another MatOfInt4 using a for loop with lines2.put(i,0, lines.get(i, 0));
Below is the piece of code and the log outputs.  There are no compiler errors or runtime errors, just no values saved in the lines2 destination MatOfInt4. Either I have misunderstood how the put method works or there is something else wrong.
Does anyone know where this could be going wrong?  Any experts with Java and OpenCV able to give any guidance or corrections to the code below so that it will work?
You can see lines has 180 entries, the for loop counts 180 loops but lines2 is empty when it's finished.  I can get() the values correctly from lines and I'm using the values in other parts of the code, but I can't put() anything into lines2.
MatOfInt4 lines = new MatOfInt4();
lines = houghLinesP(sub);
Log.i(TAG, "Total Sub Lines Returned: " + lines.size());
MatOfInt4 lines2 = new MatOfInt4();
for(int i = 0; i < lines.rows(); i++) {
lines2.put(i,0, lines.get(i, 0));
Log.i(TAG, "Count of loop: " + i);
}
Log.i(TAG, "Dump of lines2 Returned: " + lines2.dump());
Log.i(TAG, "Total Lines2 Returned: " + lines2.size());
Results of test code:
Total Sub Lines Returned: 1x180
Dump of lines2 Returned: []
Count of loop: 180
Total Lines2 Returned: 0x0
Any help or guidance would be greatly appreciated.
The lines2 matrix has no size, so you cannot set values in it. To solve the problem you can allocate the matrix with the appropriate size, e.g.
MatOfInt4 lines2 = new MatOfInt4();
lines2.create(1,lines.rows(), lines.type());
// alternatively:
Mat lines2 = new Mat(1,lines.rows(), lines.type());
If you don't do anything else in the loop you could also just transpose the matrix to achieve the same result:
Mat lines2 = lines.t();
This transposed matrix will already contain the correct values. For my example image this produces the following output:
Total Sub Lines Returned: 1x295
Dump of lines2 Returned: [9, 187, 137, 201, 353, 211, 430, 213, 95, etc...
Total Lines2 Returned: 295x1

Start Java Application using System.Diagnostics.Process

$startCount = 5;
$processors = Get-WmiObject -Class Win32_ComputerSystem | select -
ExpandProperty "NumberOfLogicalProcessors";
$perInstance = $processors / $startCount;
$affinityloop = 1;
$affinityvalue = 0;
for($i=1; $i -le $startCount; $i++) {
$affinityvalue = 0;
for($u=1; $u -le $perInstance ; $u++) {
$affinityvalue = $affinityvalue + $affinityloop;
$affinityloop = $affinityloop * 2;
}
$ps = new-object System.Diagnostics.Process;
$ps.StartInfo.Filename = "C:\jdk\bin\java.exe";
$ps.StartInfo.Arguments = " -server -Xms10240m -Xmx10240m -XX:+UseG1GC -cp `".;*`" Worker";
$ps.start();
Write-Host $affinityvalue ;
$ps.ProcessorAffinity = [Convert]::ToString($affinityvalue, 16);
}
I am trying to start a Windows Java application on two different NUMA nodes. Has anyone able to accomplish this with Diagnostics.Process format? This code does work fine, but all applications created are on the same NUMA node and hence only using 50% power.
I tried codes such as $mod = ($i % 2); to help prepare for some way to alternate the NUMA node but unsure how to specify it within the script.
EDIT
Tried to load balance with ProcessorAffinity and I get crashes due to Double unable to convert to IntPtr
EDIT 2
So it looks like Windows Groups the processors past 64 into Groups. Can't seem to find a way to alter that like ProcessAffinity. I guess its just not that common. You can easily do it in Task manager, just need to find a way to do it in Powershell or similar.

Compiling/Running Java files in Terminal

I'm attempting to run a file that calls multiple files, but I'm getting some errors.
Inside the current directory called day4Measurement, I have 13 files: BuggyMeasurement.java, BuggyMeasurement01.java, BuggyMeasurement02.java, BuggyMeasurement03.java, BuggyMeasurement04.java...BuggyMeasurement10.java, MeasurementTest.java, and Measurement.java.
Measurement.java contains the main() and calls all the other files.
Here's the main():
public static void main(String [] args){
JUnitCore tester = new JUnitCore();
String s = "Failed to detect: ";
int count = 0;
String [] tests = {"toString prints reverse inches then feet", // 01
"plus modifies this", // 02
"minus modifies this", // 03
"multiple modifies this", // 04
"plus incorrect roll over", // 05
"minus incorrect roll over", // 06
"multiple incorrect roll over", // 07
"plus incorrect non-roll over", // 08
"minus incorrect non-roll over", // 09
"multiple incorrect non-roll over", // 10
"CORRRRECTTT!!!"
};
for (int i = 1; i < tests.length + 1; i++){
testRound = i;
System.out.println("Running: " + tests[i-1]);
TestRunner.run(day4Measurement.MeasurementTest.class);
Result temp = tester.run(day4Measurement.MeasurementTest.class);
if (temp.wasSuccessful()) {
s += tests[i-1] + "; ";
count++;
}
}
System.out.print(10-(count-1)*0.5 + " ");
System.out.println(s);
}
In the Mac Terminal, I run
javac Measurement.java
and I get issues. Here's what I get:
Any suggestions?
Once you have all the files in a directory (they may be in subdirectories - as long as they are all inside some shared directory), let's call it dir, use the following:
javac -classpath dir Measurement.java
Assuming that you are running the command from the same directory that Measurement.java is in. If not, and either way you are safer this way, make it an explicit path to both dir and Measurement.java, such as:
javac -classpath /home/yourusername/dir /home/yourusername/dir/Measurement.java
This says to the java compiler "I want to compile Measurement.java, here's where it is, and here's where you can find all the class and/or source files that it needs." It will then compile only the files referred to by Measurement.java, so you don't need to worry about accidentally compiling all of your java files ever.
One thing to check would be to make sure your directory structure for those files mirrors the package structure. For example, if package for class ABC is com.foo, then your ABC.java file should live in com/foo folder.
Compile all the files with javac *.java

Categories

Resources