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

余生颓废 提交于 2019-12-30 11:23:10

问题


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 to use a variable in a single terminal command? :S

The command I'm trying to improve is of this form: javac Main.java && java Main

I only want to change the reference to Main once instead of typing it twice each time.


回答1:


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




回答2:


You could set it as an environment variable. Something like,

export CLS=Main # Change Main once
javac $CLS.java && java $CLS # Will use "Main"



回答3:


Sometimes the simplest approach is best. It sounds like you have a bunch of programs to compile and run. I would suggest you put those names into a text file, like this:

Main
Foo
Bar

and then use some search and replace in the editor to make the text like this:

javac Main.java && java Main
javac Foo.java && java Foo
javac Bar.java && java Bar

|Then just select the lines to run and paste them into the terminal.

Of course, it depends on your expected workflow, but sometimes the simplest is the best.



来源:https://stackoverflow.com/questions/37745334/how-to-reference-main-once-only-in-this-ubuntu-terminal-command-javac-main-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!