Home > Back-end >  Understanding java command line
Understanding java command line

Time:02-13

Can someone help me understand what the java cmd line is doing below (broken down so easier to read)? I found this in our AV and have no clue what this cmd does as a whole.

From my googling, I found that for -DACTIVE=true, the -D sets a system property value. In this case, my understaning is that it's setting the ACTIVE value to true..?

I also understand that -classpath searches for class files in the specified directories.

Unfortunately for com.zerog.lax.LAX , I can't find what it does/is. Any help is apreciated.

    "C:\Users\user\AppData\Local\Temp\I1644615473\Windows\resource\jre\bin\java.exe" -DACTIVE=true 
    
    -classpath "C:\Users\user\AppData\Local\Temp\I1644615473\InstallerData\IAClasses.zip;
    
    C:\Users\user\AppData\Local\Temp\I1644615473\InstallerData\Execute.zip;
    
    C:\Users\user\AppData\Local\Temp\I1644615473\Windows\InstallerData\Execute.zip;

C:\Users\user\AppData\Local\Temp\I1644615473\InstallerData\Resource1.zip;
    
    C:\Users\user\AppData\Local\Temp\I1644615473\Windows\InstallerData\Resource1.zip;
    
    C:\Users\user\AppData\Local\Temp\I1644615473\InstallerData;
    
    C:\Users\user\AppData\Local\Temp\I1644615473\Windows\InstallerData;" 
    
    com.zerog.lax.LAX 
    
    "C:/Users/user/AppData/Local/Temp/I1644615473/Windows/setup.lax" 
    
    "C:/Users/user/AppData/Local/Temp/laxF375.tmp"

CodePudding user response:

There are basically 4 parts to that:

  1. The java command and its options (in this case, -D, which sets an arbitrary 'property'; the running program presumably will want this)

  2. The classpath, which tells where to find all code that is required for execution. It is a list of directories and/or jar files.

  3. The class to be run - in this case the class com.zerog.lax.LAX, which is to be found somewhere in the places listed in classpath, and which must contain a suitable main() method.

  4. Arguments to be passed to the main() of com.zerog.lax.LAX.

CodePudding user response:

Directly from java -help:

C:\Users\Hector>java -help
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32          use a 32-bit data model if available
    -d64          use a 64-bit data model if available
    -server       to select the "server" VM
                  The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.

  1. -D<name>=<value> Sets a system property. In your case: ACTIVE=true Here's the thing: If "ACTIVE" is not a known system property, you must define it somewhere. For Maven projects, I would set them up in the POM. Here's how you do that in Maven: https://stackoverflow.com/a/3231981/2851311
  2. -classpath or -cp is the preferred way to specify the classpath for the application being run. Why is this the preferred way? Simply because, if you want to execute a program from many machines, you would have to either set the classpath on every single one, OR simply create a script with the -classpath or -cp option in the command line instruction and you will not have to worry about the targeted system not being correctly configured to find the compiled unit being executed. Here's more on the topic of using these options: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

How is this information useful to you?

When you want to execute java.exe from a directory, the Operating System (OS) must know where to find this executable file. You could have many files with the same name in the system. The OS resolves this in this matter. First, it assumes the .exe file is in the same directory from where you attempted to execute it. Then, if it cannot find it there, it will go to all the directories listed in the classpath set on the file system in the order they appear and will execute the first one it finds. If no match is found, it throws an error.

You can, alternatively, provide the full path of the location of the .exe file. This is the most reliable way to execute the .exe file that you want to use.

I used this illustration to help you understand that this is the same with the program you want to launch. When you execute something like

C:\My Directory\> java HelloWorld

The OS expects that either the compiled unit is in "My Directory" or it can find it somewhere in the classpath. Java provides a mechanism to give you the flexibility to find the compiled unit. And this is by using either the -cp or -classpath option. All that said, this only works if your compiled unit is simple and don't have any dependencies and/or the compiled unit is not in a JAR file.

If your program has dependencies, the same concept applies: The OS must be able to find these dependencies in order to load them at runtime. If the program is inside a JAR file, you must execute the JAR instead of the compiled (main) class directly. To execute the JAR, you must use the -jar option.

java -jar Example.jar

If your program requires arguments, you need to pass them from the command line as well. More on this topic can be found here: https://www.baeldung.com/java-run-jar-with-arguments

  • Related