How to reference Main once only in this ubuntu terminal command: “javac Main.java && java Main”?

后端 未结 3 666
悲哀的现实
悲哀的现实 2021-01-15 10:56

I am reviewing a number of different java programs and am trying to work out how I can update the reference to the program name once only instead of twice. Is there a way t

3条回答
  •  遥遥无期
    2021-01-15 11:21

    You can do it with one line like this:

    PC=com/mycompany/Main && CN=$(echo $PC | tr / .) && javac $PC.java && java $CN
    

    This will work even if you have a package name as it will automatically replace / with . to build properly the full qualified name of your class. In the example above it will compile the class com/mycompany/Main.java then launch com.mycompany.Main

    PC: Refers to the relative path of your class without .java, this what you will need to change

    CN: Refers to the full qualified name of your class automatically built from the previous path

提交回复
热议问题