2

I have a set of instructions to create a Java application that takes multiple arguments when running the application from a CMD line.

The instructions state:

Thus, using the example set of above, assuming the main() method of your program is in a class called JavaClassName, the output should be:

$ java JavaClassName 4 7 file.csv
program output here

My question is:

Isn't this skipping the compile process?
Would they assume that loading the Java classes onto a computer that has never run this application before (or a directory with purely the .java files needed to run); running the cmd

$ java JavaClassName 4 7 file.csv

would output something?

Sidenote: Currently, running that CMD outputs

Error: Could not find or load main class JavaClassName

Have ran through multiple SO questions and online tutorials trying to get this to even run but I have yet to get it to work.

6
  • If you give them the compiled class files, they should run with that command. What filetype are they asking for? If they ask for the source code, they probably know how to compile it and it's implied Commented Jan 30, 2018 at 18:03
  • 2
    The example isn't in a class called JavaClassName if it is in a ".java" file. Yes, you must compile ".java" files to be ".class" files (and then you can package those in a ".jar" file) before you can run it with Java. Try javac -cp . JavaClassName.java and then your example command. Commented Jan 30, 2018 at 18:03
  • They are asking for the files submitted in a zip file, so I assume the compiled class files, as well as the source .java files Commented Jan 30, 2018 at 18:05
  • Just include the java and class files in your submission, if their process is to compile the source code, then your class files will be overwritten and no harm done. If they don't compile your source code, then the class files will still be there ready to run Commented Jan 30, 2018 at 18:07
  • This is also why it's common to use/request a make file with run and compile targets Commented Jan 30, 2018 at 18:08

2 Answers 2

3

from Java 10 it is possible to run java programs that fit a single file without manually run the compiler first.
It will be compiled on the fly before execution. Nice and useful for scripting.

e.g. writing HelloWorld.java file

public class HelloWorld{
  public static void main(String[] args) { 
        System.out.println("Hello World");
  }
}

we can run it as

java HelloWorld.java

We can also add more classes in the single file.


from Java 11 we can also use shebang files

  • we have to specify which version of the java language you want to use
  • we have to save the file without .java extension
  • remember to give executable permissions chmod +x HelloWorld

writing HelloWorld file

#!/path/to/java --source 11

public class HelloWorld{
  public static void main(String[] args) { 
        System.out.println("Hello World");
  }
}

and we can run it as

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

Comments

2

You ask:

Isn't this skipping the compile process?

Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.

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.