• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Execute Command  RSS feed

 
O Mahajan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Liutauras Vilda
Marshal
Posts: 4802
330
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I was asked what does the & do in the above command.
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Carey Brown
Saloon Keeper
Posts: 3246
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 56220
171
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!