1

What is the command-line equivalent (Linux) of

gcc -o executable file.c

for a jar file? (Java instead of gcc)

I know it is possible to do because at my school, all that is necessary is writing "mars" (the MIPS simulator) in command-line

4 Answers 4

3

To execute a Java JAR file, run the following command, replacing YourJarFile.jar with the filename of your JAR file.

java -jar YourJarFile.jar

EDIT: There is no way to execute a Java JAR like

./YourJarFile.jar

because the Java Runtime Environment must interpret the JAR file, not the operating system's sh.

Sign up to request clarification or add additional context in comments.

5 Comments

I think the poster is asking about creating the jar, not executing it.
This is, by the way, the closest approximation you're going to get to an executable. Java doesn't do executables.
I know, that is what I have been doing, but I want to be able to do it with one word. I my c example, I would be able to write ./executable to run my program.
@spatara: You can't. There is no way to do it with java and javac. I think gcj might generate executables, though.
You could, of course, create a shell script that runs the appropriate java command.
1

There is no direct, native, command-line equivalent. @Deltik provides the closest compatible method.

Comments

0
javac myfile.java

presuming you have a JDK installed ;)

Comments

0

To compile Foo.java and Bar.java:

javac Foo.java Bar.java

To put the compiled class files in a jar:

jar cvf classes.jar Foo.class Bar.class

Note that if you put your classes in packages, which you should, your class files will be in a sub-directory and you will have to change the jar command accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.