Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java Execute Command

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recently in a interview I was asked about command for executing a java code. The command is as shown as follows

$java FileName&

Does anyone know anything about this command? I looked up about this command but couldn't find any information about it.
 
Marshal
Posts: 8435
606
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O Mahajan wrote:Recently in a interview I was asked about command for executing a java code.

What is the full questions? I suspect you're not telling us full details, or at least taking some words out of context by making a wrong assumptions that these are not important.

Please tell exact question and in which context it was asked. About what kind of operating systems you were talking?
 
O Mahajan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asked what does the & do in the above command.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O Mahajan wrote:I was asked what does the & do in the above command.



This question has nothing to do with Java. It has to do with the operating system. In pretty much most OSes, including Windows, Linux, Solaris, and other forms of Unix shells, the & runs the command in the background.

Henry
 
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O Mahajan wrote:I was asked what does the & do in the above command.

On UNIX like OSs, the & puts the process in background and immediately returns you to the command line prompt.
 
Marshal
Posts: 76491
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, but will the java tool abey the & symbol?

Yes, it does. It means the process starts but does not return control to the terminal. If you push the return key afterwards, however, you get control back.
 
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe, but will the java tool abey the & symbol? ... the process starts but does not return control to the terminal. If you push the return key afterwards, however, you get control back.


I'm pretty sure that the "&" doesn't make it past the OS Shell; the Java runtime knows nothing about the "&" by the time it executes the program. You can easily test that by printing out the number of arguments passed into main. To prevent having to hit Enter/Return after putting the process in the background, you need to redirect standard output to something other than the console, such as /dev/null or a file. See the explanation by Walter Brameld IV here: http://tldp.org/LDP/abs/html/x9644.html
 
Campbell Ritchie
Marshal
Posts: 76491
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right; the ampersand never seems to make it from the shell to the JVM. I have a program which prints the ASCII values of chars.

campbell@campbellsComputer:~/java$ java CharacterPrinter &
[1] 3327
campbell@campbellsComputer:~/java$
[1]+ Done java CharacterPrinter
campbell@campbellsComputer:~/java$ java CharacterPrinter \&
& = 0x0026

If I use & it runs and terminates but I have to use enter to reactivate the shell. If I convert the & to an escape sequence it prints it as 0x26. If I put & in the middle of a word it interprets that as terminating the command and what follows produes a “command not found” error.

If I have something which goes into an infinite loop (‍) or starts another thread (Swing) I can regain control of the shell by using & and (later) enter.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic