vscode coderunner has trouble executing java code - java

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

Related

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.

Unable to resolve class com.cloudbees.hudson.plugins.folder.Folder

I am trying to gather data from jenkins using groovy script and getting an error:
unable to resolve class com.cloudbees.hudson.plugins.folder.Folder
Below is the code:
import jenkins.model.*
import hudson.model.*
import groovy.time.TimeCategory
use ( TimeCategory ) {
// e.g. find jobs not run in last 1 year
sometimeago = (new Date() - 1.year)
}
jobs = Jenkins.instance.getAllItems()
lastabort = null
jobs.each { j ->
if (j instanceof com.cloudbees.hudson.plugins.folder.Folder) { return }
numbuilds = j.builds.size()
if (numbuilds == 0) {
println 'JOB: ' + j.fullName
println ' -> no build'
return
}
lastbuild = j.builds[numbuilds - 1]
if (lastbuild.timestamp.getTime() < sometimeago) {
println 'JOB: ' + j.fullName
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result + ', time: ' + lastbuild.timestampString2
}
}
The error is:
rg.codehaus.groovy.control.MultipleCompilationErrorsExceptio‌​n:
startup failed: Script1.groovy: 12: unable to resolve class
com.cloudbees.hudson.plugins.folder.Folder # line 12, column 20. if (j
instanceof com.cloudbees.hudson.plugins.folder.Folder) { return } ^ 1
error at
org.codehaus.groovy.control.ErrorCollector.failIfErrors(Erro‌​rCollector.java:302)
I see Folder.java in jenkinsci/cloudbees-folder-plugin.
That means you need to:
check if you do have JENKINS/CloudBees Folders Plugin installed, or your groovy script would not be able to resolve that dependency.
Add "import com.cloudbees.hudson.plugins.folder.*" to be sure the script is able to make the instanceOf work.
When running groovy scripts that import libraries in Jenkins, check that your Jenkins build step is an "Execute system Groovy script", not a plain old "Execute Groovy script".
The 'system' scripts run on the existing JVM, as opposed to spawning a new one and therefore losing access to the shared libraries available to the original Jenkins JVM instance.
Groovy Script vs System Groovy Script - https://plugins.jenkins.io/groovy/

How to train Chunker in Opennlp?

I need to train the Chunker in Opennlp to classify the training data as a noun phrase. How do I proceed? The documentation online does not have an explanation how to do it without the command line, incorporated in a program. It says to use en-chunker.train, but how do you make that file?
EDIT: #Alaye
After running the code you gave in your answer, I get the following error that I cannot fix:
Indexing events using cutoff of 5
Computing event counts... done. 3 events
Dropped event B-NP:[w_2=bos, w_1=bos, w0=He, w1=reckons, w2=., w_1=bosw0=He, w0=Hew1=reckons, t_2=bos, t_1=bos, t0=PRP, t1=VBZ, t2=., t_2=bost_1=bos, t_1=bost0=PRP, t0=PRPt1=VBZ, t1=VBZt2=., t_2=bost_1=bost0=PRP, t_1=bost0=PRPt1=VBZ, t0=PRPt1=VBZt2=., p_2=bos, p_1=bos, p_2=bosp_1=bos, p_1=bost_2=bos, p_1=bost_1=bos, p_1=bost0=PRP, p_1=bost1=VBZ, p_1=bost2=., p_1=bost_2=bost_1=bos, p_1=bost_1=bost0=PRP, p_1=bost0=PRPt1=VBZ, p_1=bost1=VBZt2=., p_1=bost_2=bost_1=bost0=PRP, p_1=bost_1=bost0=PRPt1=VBZ, p_1=bost0=PRPt1=VBZt2=., p_1=bosw_2=bos, p_1=bosw_1=bos, p_1=bosw0=He, p_1=bosw1=reckons, p_1=bosw2=., p_1=bosw_1=bosw0=He, p_1=bosw0=Hew1=reckons]
Dropped event B-VP:[w_2=bos, w_1=He, w0=reckons, w1=., w2=eos, w_1=Hew0=reckons, w0=reckonsw1=., t_2=bos, t_1=PRP, t0=VBZ, t1=., t2=eos, t_2=bost_1=PRP, t_1=PRPt0=VBZ, t0=VBZt1=., t1=.t2=eos, t_2=bost_1=PRPt0=VBZ, t_1=PRPt0=VBZt1=., t0=VBZt1=.t2=eos, p_2=bos, p_1=B-NP, p_2=bosp_1=B-NP, p_1=B-NPt_2=bos, p_1=B-NPt_1=PRP, p_1=B-NPt0=VBZ, p_1=B-NPt1=., p_1=B-NPt2=eos, p_1=B-NPt_2=bost_1=PRP, p_1=B-NPt_1=PRPt0=VBZ, p_1=B-NPt0=VBZt1=., p_1=B-NPt1=.t2=eos, p_1=B-NPt_2=bost_1=PRPt0=VBZ, p_1=B-NPt_1=PRPt0=VBZt1=., p_1=B-NPt0=VBZt1=.t2=eos, p_1=B-NPw_2=bos, p_1=B-NPw_1=He, p_1=B-NPw0=reckons, p_1=B-NPw1=., p_1=B-NPw2=eos, p_1=B-NPw_1=Hew0=reckons, p_1=B-NPw0=reckonsw1=.]
Dropped event O:[w_2=He, w_1=reckons, w0=., w1=eos, w2=eos, w_1=reckonsw0=., w0=.w1=eos, t_2=PRP, t_1=VBZ, t0=., t1=eos, t2=eos, t_2=PRPt_1=VBZ, t_1=VBZt0=., t0=.t1=eos, t1=eost2=eos, t_2=PRPt_1=VBZt0=., t_1=VBZt0=.t1=eos, t0=.t1=eost2=eos, p_2B-NP, p_1=B-VP, p_2B-NPp_1=B-VP, p_1=B-VPt_2=PRP, p_1=B-VPt_1=VBZ, p_1=B-VPt0=., p_1=B-VPt1=eos, p_1=B-VPt2=eos, p_1=B-VPt_2=PRPt_1=VBZ, p_1=B-VPt_1=VBZt0=., p_1=B-VPt0=.t1=eos, p_1=B-VPt1=eost2=eos, p_1=B-VPt_2=PRPt_1=VBZt0=., p_1=B-VPt_1=VBZt0=.t1=eos, p_1=B-VPt0=.t1=eost2=eos, p_1=B-VPw_2=He, p_1=B-VPw_1=reckons, p_1=B-VPw0=., p_1=B-VPw1=eos, p_1=B-VPw2=eos, p_1=B-VPw_1=reckonsw0=., p_1=B-VPw0=.w1=eos]
Indexing... done.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at opennlp.tools.ml.model.AbstractDataIndexer.sortAndMerge(AbstractDataIndexer.java:89)
at opennlp.tools.ml.model.TwoPassDataIndexer.<init>(TwoPassDataIndexer.java:105)
at opennlp.tools.ml.AbstractEventTrainer.getDataIndexer(AbstractEventTrainer.java:74)
at opennlp.tools.ml.AbstractEventTrainer.train(AbstractEventTrainer.java:91)
at opennlp.tools.ml.model.TrainUtil.train(TrainUtil.java:53)
at opennlp.tools.chunker.ChunkerME.train(ChunkerME.java:253)
at com.oracle.crm.nlp.CustomChunker2.main(CustomChunker2.java:91)
Sorting and merging events... Process exited with exit code 1.
(My en-chunker.train had only the first 2 and last line of your sample data set.)
Could you please tell me why this is happening and how to fix it?
EDIT2: I got the Chunker to work, however it gives an error when I change the sentence in the training set to any sentence other than the one you've given in your answer. Can you tell me why that could be happening?
As said in Opennlp Documentation
Sample sentence of the training data:
He PRP B-NP
reckons VBZ B-VP
the DT B-NP
current JJ I-NP
account NN I-NP
deficit NN I-NP
will MD B-VP
narrow VB I-VP
to TO B-PP
only RB B-NP
# # I-NP
1.8 CD I-NP
billion CD I-NP
in IN B-PP
September NNP B-NP
. . O
This is how you make your en-chunk.train file and you can create the corresponding .bin file using CLI:
$ opennlp ChunkerTrainerME -model en-chunker.bin -lang en -data en-chunker.train -encoding
or using API
public class SentenceTrainer {
public static void trainModel(String inputFile, String modelFile)
throws IOException {
Objects.nonNull(inputFile);
Objects.nonNull(modelFile);
MarkableFileInputStreamFactory factory = new MarkableFileInputStreamFactory(
new File(inputFile));
Charset charset = Charset.forName("UTF-8");
ObjectStream<String> lineStream =
new PlainTextByLineStream(new FileInputStream("en-chunker.train"),charset);
ObjectStream<ChunkSample> sampleStream = new ChunkSampleStream(lineStream);
ChunkerModel model;
try {
model = ChunkerME.train("en", sampleStream,
new DefaultChunkerContextGenerator(), TrainingParameters.defaultParams());
}
finally {
sampleStream.close();
}
OutputStream modelOut = null;
try {
modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
model.serialize(modelOut);
} finally {
if (modelOut != null)
modelOut.close();
}
}
}
and the main method will be:
public class Main {
public static void main(String args[]) throws IOException {
String inputFile = "//path//to//data.train";
String modelFile = "//path//to//.bin";
SentenceTrainer.trainModel(inputFile, modelFile);
}
}
reference: this blog
hope this helps!
PS: collect/write the data as above in a .txt file and rename it with .train extension or even the trainingdata.txt will work. that is how you make a .train file.

Autoit Java argument in run command through $chosen variable

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);
}
}

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