views:

99

answers:

2

I am working with telecom company. I am familiar with Java programming language. But now I have a task to write a script, with Linux operating systems. I have to write a script for fetching data from other computer and check some conditions. How can I do that using Java?

A: 

Java is not a scripting language by any definition.

Why not use a better tool for the job such as a shell script or even a simple interpreted language such as Perl, Python or Ruby?

Just to give you some incentive, here are the snippets for opening and reading a file in Python as opposed to Java.

Python:

file = open(filename, 'r')
for line in file:
    print line

Java:

Scanner scanner = new Scanner(new File(fileName), encoding);
try {
  while (scanner.hasNextLine()){
    System.out.println(scanner.nextLine() + "\n");
  }
}
finally {
  scanner.close();
}

Alternatively, if you must write in any language on top of the JVM, use Jython (Python for the JVM) or a language such as Groovy.

Yuval A
Or Groovy or JPython, if he wants to stay inside the Java infrastructure.
Joachim Sauer
+1 to counteract -1 since, although it doesn't necessarily answer the question, I think this is a good clarification and belongs with this question
Carson Myers
+3  A: 

If you really need to write a "script" and it needs to run on the Java runtime, I would recommend using Groovy. By "script" I mean,

A scripting language, script language or extension language is a programming language that allows control of one or more software applications.

If you just have to use straight Java, then you need to just write a command line program. I recommend using JSAP ( Java Simple Argument Parser ) for parsing the command line arguments, you are going to need it. And either way you go you will need to bundle this Java code as an application. Here is a post on how to build an executable .jar file, so you can deploy your application anywhere without having to set up a bunch of CLASSPATH voodoo.

fuzzy lollipop