Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to convert your java program so that it runs as a batch program i.e it can be run like a command in the ubuntu or any linux machine. What is the procedure and what are the steps to follow? Specifically java program. C program is also fine.

I want to know if any libraries to be added to the java/c program and also where to place the program and how to write a man page.

share|improve this question
    
There is no such thing as a batch program in Linux. This is for windows users. The closest thing near it is a bash (shell) script –  NlightNFotis Oct 5 '12 at 11:14
    
sorry my bad. I am new to linux –  thanmai Oct 5 '12 at 11:16
    
Another related technique to batch is awk scripting. –  cauon Oct 5 '12 at 11:18
1  
@cauon Awk is just another tool for scripting. As are bash, python, perl, ruby, and the list goes on and on... –  NlightNFotis Oct 5 '12 at 11:29

2 Answers 2

up vote 6 down vote accepted

You can not "convert" a program written on C or Java in order to run as a bash script. Bash uses different syntax, so most likely you will have to rewrite the logic on a bash script.

Now if you are only asking about how to execute a program written on Java/C via the command line, then the answer is pretty simple.

For C:

Editor Choice

Now in order to write a C program in linux, all you need to do is to fire up an editor (gedit is a very good gui editor, nano is good for beginners with the command line, vim/emacs for experienced programmers), like this: gedit myprogram.c or vim myprogram.c and start coding. Below is an example of an text editor editing source a Hello World program in C on my computer in the Ubuntu terminal.

VIM editing a basic Hello World program written in C

NOTE:You can also use a piece of software called an IDE (Integrated Development Environment) to do C programming. Popular IDEs include code::blocks, Eclipse with a plugin called CDT (C/C++ Development tools) and my all time favorite for newbies and starters Geany (although one could argue that Geany is more of an editor, than an IDE, but whatever).

C Standard Library

The standard C Library (glibc) is pretty much in every Linux or Unix system there is and is POSIX compliant. You can find most of in /usr/include. A fine trick to find where headers (libraries) you are interested in exist in your system is firing up a terminal and typing whereis followed by the header filename like this: whereis stdio.h

Compilation

Supposing you have created a file called myprogram.c that has c source code in it, then you compile it first via a compiler (GCC belonging to the GNU toolchain is contained in nearly every linux system. I am not a 100% sure about this, but I believe that POSIX instructs that there are such tools in the basic OS) like this: gcc myprogram.c and then you execute the executable (in this case a.out, since we did not define an executable output name to the compiler) like this: ./a.out

For Java:

Java Development Kit

To begin with java development you must make sure there is at least one version of the Java Development Kit (JDK). To make sure you have jdk installed, open up your Ubuntu Software Center, and Type in the Search box Java Development Kit.

enter image description here

If you see at least one package with "Java Development Kit" or JDK in its name marked with a tick, then this means that you have JDK installed.

WARNING: You need to have a JDK version that supports the version of Java you want to program in and above, otherwise you won't be able to work properly. For instance a JDK that supports Java v6 can compile java source code with features up until Java 6 BUT NOT java 7. A jdk that supports Java version 7 can compile source code that has features up until version 7 (including 6).

SECOND WARNING: You MUST make sure that you have at least one version of a JDK installed. If you see a package named JRE you this doesn't apply to your needs. JRE stands for Java Runtime Environment and you need it to run Java applications. In order to program them, you need a Java Development Kit. A Java Development Kit also includes many of the tools found in Java Runtime Environments.

Editor (IDE)

You may be able to follow the C like code writing technique for java (firing up any text editor and start typing), but for java, such techniques are impractical for serious development. This is why many Java developers use tools named IDE s which stand for Integrated Development Environment that make your life easier when you write source code by attempting to support all the features you might want to have (including, but not limited to, code completion, a debugger, a profiler, compiler integration, etc) under one piece of software. In Ubuntu software centre you can find 3 very good IDEs that you can choose from. The first one is Netbeans which is very friendly to beginners (well, it is at least friendlier than the other too) while being fairly powerful. If you feel like you want more, or are more advanced Java developer you can choose between Eclipse (very popular among Java Developers worldwide on any platform) and IntelliJ IDEA Community Edition by JetBrains which is one nice and powerful Java IDE.

Below is a screenshot of Eclipse, a fairly popular Java IDE.

enter image description here

Compilation

Now, to compile your java software files (assuming myprogram.java source code file) with the java compiler like this: javac myprogram.java and then you find the compiler output (should be a .class file, in this case myprogram.class) and call the java virtual machine to process it like this: java myprogram.class

share|improve this answer
    
And the no conversion rule acts for all dissimilar languages, of course/ –  hexafraction Oct 5 '12 at 11:16
    
@ObsessiveFOSS My thoughts exactly. However, in my opinion, logic is far more important than syntax. Logic is portable amongst different syntax languages. –  NlightNFotis Oct 5 '12 at 11:17

C is native in Linux, so you don't need extra libraries. You just compile (using gcc for example), and run your newly created file. See: http://www.codecoffee.com/tipsforlinux/articles/18.html

For java, you need to use java -jar youJavaJar.jar to execute it. I don't think it's possible to compile for native Linux.

For your man page, have a look at this: http://www.cyberciti.biz/faq/linux-unix-creating-a-manpage/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.