Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Does Java lack a feature like Shell scripts which another JVM language Scala supports?

 
Ranch Hand
Posts: 1410
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scala is also a JVM language like java and it supports shell scripts. Does java lack such a feature?
 
Sheriff
Posts: 7051
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "supports shell scripts" what do you mean exactly?
 
Monica Shiralkar
Ranch Hand
Posts: 1410
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This kind of script:



 
Saloon Keeper
Posts: 21960
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is a compiled language, not a scripting language, so no, you cannot do that in Java. You can execute Java apps from a script though. For example, that's exactly how the Tomcat web application server program runs.

To "script Java", there's groovy.
 
Monica Shiralkar
Ranch Hand
Posts: 1410
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Scala is also a compiled language.
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the point to this. Use a tool the way it's meant to be used. You don't use a hammer to drive in a screw, you use a screwdriver. Are you trying to get someone to say that Java is somehow less than Scala because of its lack of scripting support? Please stop being coy and get to the point.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:This kind of script:


For example, instead of just showing us this "scala script," why don't you try something like it with jshell? You should know that jshell wasn't meant to be used as a scripting tool though.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you're looking for JVM-based scripting languages besides Scala, you could look into Groovy and Kotlin. Both of those have built-in scripting support and they compile to JVM bytecode.
 
Saloon Keeper
Posts: 11881
253
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhh who says you can't?

Not that there's much point to doing this though. As others have said, just use the compiler for your applications, and if you quickly want to try out some code you can use jshell.
 
Monica Shiralkar
Ranch Hand
Posts: 1410
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. We can do it using JShell.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks. We can do it using JShell.


Again, JShell wasn't meant for scripting purposes, it was meant as a quick and dirty way to try out small code snippets without having to go through the hassle of writing a full blown class and then go through the compile/run process.

Also, what's wrong with just invoking java the regular way from a shell script:

This is a more common usage pattern that trying to execute Java as a shell script.
 
Rancher
Posts: 3503
37
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both interactive shell (jshell or repl) and single-source-file shebang execution are things that java did not have originally, and when Scala and other languages appeared with these features, some people found them useful.  Eventually, both features were added to Java as well.  JShell appeared in Java 9, and single source file / shebang execution appeared in Java 11.  Older articles and books won't know about them.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike is right. Here's a newer article that explains it quite well, in my opinion: https://blog.codefx.org/java/scripting-java-shebang/
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic