0

I have a very simple java code - I'm just learning Java.

However I don't want to have to retype javac file.java and java file again and again for a little change.

Is there a way to do it faster? Or a one-liner that I can quickly copy & paste?

  • 2
    I don’t know Java at all, but couldn’t you just run `javac file.java && java file` as a one liner, then press up in the terminal to get the last entered command and not have to retype it? – Andy Aug 17 '19 at 16:35
  • 2
    Or put the above into a batch script. – Tim Biegeleisen Aug 17 '19 at 16:36
  • 2
    IDEs will also automatically do stuff like this for you. Learning the low level stuff like this is good, but using good tools will make your life easier. – Carcigenicate Aug 17 '19 at 16:36
  • 1
    Which editor are you using? There may be a convenient way to run the code from the editor. – user202729 Aug 17 '19 at 16:37
  • Sublime text. It doesn't have a build option for Java. I have VSCode and I don't know if that does it, but @Andy 's solution is what I wanted. If he'll write it up as an answer, I can accept it. Of course, if there's some advantages for Java for coding from VScode, I'll use that. –  Aug 17 '19 at 16:40
  • Possible duplicate of [Compiling and Running Java Code in Sublime Text 2](https://stackoverflow.com/questions/10560295/compiling-and-running-java-code-in-sublime-text-2) – user202729 Aug 18 '19 at 09:58

2 Answers2

2

You can use an IDE, such as Eclipse or NetBeans, to directly run without typing anything. This is because the IDE automatically recompiles changed code for you.

Download link for Eclipse: https://www.eclipse.org/downloads/packages/release/kepler/sr1/eclipse-ide-java-developers

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • Well, I don't think its fully feasible to download a whole IDE for this. –  Aug 17 '19 at 16:44
  • Sure it is feasible. And you get a lot more benefit than just this. (Almost nobody does serious Java dev using the command line and a text editor these days.) – Stephen C Aug 17 '19 at 16:52
  • Yeah, I have Visual Stdio but I'm not serious yet. –  Aug 17 '19 at 17:22
1

You can run both commands as a one-liner, using && so that the second command will only run if the first was successful:

javac file.java && java file

Then you can just press up in the terminal window to access the command without having to retype it.

Andy
  • 4,901
  • 5
  • 35
  • 57