How to Run Java .Class Files

How do I run Java .class files?

If your class does not have a package, you only need to set the classpath to find your compiled class:

java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2

If your class has a package, then it needs to be in a directory corresponding to the package name, and the classpath must be set to the root of the directory tree that represents the package.

// Source file HelloWorld2/src/com/example/HelloWorld2.java
package com.example;
...

Compiled class file: HelloWorld2/bin/com/example/HelloWorld2.class

$ java -cp HelloWorld2/bin com.example.HelloWorld2

How can I open Java .class files in a human-readable way?

jd-gui is the best decompiler at the moment. it can handle newer features in Java, as compared to the getting-dusty JAD.

How do I run a Java program from the command line on Windows?

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This displays the directory contents. You should see
filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the
next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see
filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program
output.

If the system cannot find javac, check the set path command. If javac
runs but you get errors, check your Java text. If the program
compiles but you get an exception, check the spelling and
capitalization in the file name and the class name and the java
HelloWorld command. Java is case-sensitive!

How to run java class file which is in different directory?

In my program com.bsoft.conc is a package name where my class file for the compiled program will be stored.If I have to run that from home folder we have to specify
java -classpath test\src com.bsoft.conc."class-file-name"

This is because we need to tell the JVM where it has to look for class file.

so , we have to specify navigation to the src using "test\src" and then
class file location "com.bsoft.conc.class-file-name"

If you have set environment variable in advanced settings then it will also be overriden if you specify classpath in cmd

Running a compiled java .class file from within java code and capturing output

Once it's compiled, you will need to load the Class object and then invoke the main(String[]) method. To capture the stdout, you will need to use System.setOut.

private String invokeClass(String className) throws URISyntaxException, IOException, ReflectiveOperationException {
Class<?> clazz = Class.forName(className);
// Alternatively, you can load the new class with a new Classloader, if you don't want to pollute the current Classloader
// Class<?> clazz = new URLClassLoader(new URL[]{getClass().getClassLoader().getResource("").toURI().toURL()}, getClass().getClassLoader()).loadClass(className);
Method main = clazz.getDeclaredMethod("main", String[].class);
try ( ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(out)) {
System.setOut(ps);
main.invoke(main, new Object[]{new String[0]});
return out.toString();
}
finally {
// Reset to the console
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
}
}

How do I run .class files on windows from command line?

The Java application launcher (a.k.a java.exe or simply java) supports up to four different ways to specify what to launch (depending on which Java version you use).

  1. Specifying a class name is the most basic way. Note that the class name is different from the file name.

     java -cp path/to/classFiles/ mypackage.Main

    Here we start the class mypackage.Main and use the -cp switch to specify the classpath which is used to find the class (the full path to the class mypackage.Main will be path/to/classFiles/mypackage/Main.class.

  2. Starting a jar file.

    java -jar myJar.jar

    This puts the jar itself and anything specified on its Class-Path entry on the class path and starts the class indicated via the Main-Class entry. Note that in this case you can not specify any additional class path entries (they will be silently ignored).

  3. Java 9 introduced modules and with that it introduce a way to launch a specific module in a way similar to how option #2 works (either by starting that modules dedicated main class or by starting a user-specified class within that module):

    java --module my.module
  4. Java 11 introduces support for Single-File Source Code Programs, which makes it very easy to execute Java programs that fit into a single source file. It even does the compile step for you:

    java MyMain.java

    This option can be useful for experimenting with Java for the first time, but quickly reaches its limits as it will not allow you to access classes that are defined in another source file (unless you compile them separately and put them on the classpath, which defeats the ease of use of this method and means you should probably switch back to option #1 in that case).

    This feature was developed as JEP 330 and is still sometimes referred to as such.

For your specific case you'd use option #1 and tell java where to look for that class by using the -classpath option (or its short form -cp):

java -classpath C:\Peter\Michael\Lazarus\ Main

If your Main.java contains the entirety of your source code (and it is in the same directory), then you can use option #4, skip the compile step and directly compile-and-execute it:

java c:\Peter\Michael\Lazarus\Main.java

How to run a java program with multiple class files without making a package?

Instead of

java -classpath "lib/*:lib/*" src/AAA.java data/*

You need to specify the compiled class to execute (not the source it came from), you don't need lib/* twice in your classpath, but you do need the current folder (actually, the folder containing your compiled classes, which is the current folder in this case). Like (change : to ; on Windows),

java -classpath "lib/*:." AAA data/*

Note: The more conventional place to put the classes would be a bin folder (you can name it whatever you like, but it's more convenient to package when the classes are all collected in a "clean" tree). So, something like

mkdir bin
javac -cp "lib/*" -d bin src/AAA.java src/MergerHTML.java
java -classpath "lib/*:bin" AAA data/*

Finally, if you're on Windows, the last command above should be

java -classpath "lib/*;bin" AAA data/*


Related Topics



Leave a reply



Submit