4

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.

turtle_in_mind
  • 986
  • 1
  • 18
  • 36

2 Answers2

1

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.
R. Horber
  • 480
  • 5
  • 11
0

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

Atmas
  • 2,389
  • 5
  • 13
  • how do i tell vscode that im in 'chapter9?" java $dir/$fileName ? – turtle_in_mind Mar 16 '21 at 02:12
  • It's best just to google how to run a Java program. When you run "java" you want to use the dot (.) syntax, not /folders. Here's one I found quickly that describes it pretty simply: https://www.tutorialspoint.com/How-to-run-Java-package-program - I updated my response above to use dot-syntax for running the app. Also review how to run javac. I'm not even sure if it does sub-directories. I've been doing IDE and frameworks for so long that running javac is addmittedly starting to be a lost art for me! – Atmas Mar 16 '21 at 23:38