0

I've just started studying java, but decided not to install JDK or JRE on my machine because I prefer portable solutions. I've simply took jre1.8.0_74 folder from that machine, where Java was installed and put it into my Eclipse folder - compiling & running well!

But after I've completed my first HelloWorld I asked myself how can I execute .class file I got?! Tried drag'n'dropping it on java.exe, javaw.exe, javaws.exe, javac.exe... nothing worked. Than I've exported project as executable .jar, but didn't managed to execute it too.

So how can I execute .class & .jar without installing Java?

Thanks

Bzhenko
  • 33
  • 1
  • 6
  • 1
    You cannot run Java programs (`.class` files and `.jar` packages) without Java(or a clone). It's as simple as that. Eclipse probably has Java included, because it's based on Java. For running your program(s), you need either a (portable) Java installation with libraries or a compiler which translates the program to native code creating an executable. – zx485 Apr 07 '16 at 16:58

2 Answers2

0

If by without installing Java you only mean not running Java installer, but still having actual Java executables available, that yes, it should be doable.

If your .class file is standalone (doesn't need other classes and libs available), it's enough to just run: /path/to/java your/package/Main where the your/package/Main path corresponds to class Main in package your.package.

Eg.

package your.package;

public class Main {
   public static void main(String[] args) {
      System.out.prinltn("Hello world");
   }
}

If your class is in the default package (you don't have the package statement), just run /path/to/java Main. if java.exe is already on your PATH, you can omit the whole path.

If your class needs some extra libs or classes in other packages you wrote, you need to set the the classpath correctly for this to work. Executable jars do the exact same thing using their MANIFEST to set the class where the main method is and the correct classpath.

See the guides for details. Also, this has been explored to death in many questions here.

If by any chance you meant without having Java available at all, then it's literally impossible to run Java applications.

Community
  • 1
  • 1
kaqqao
  • 12,984
  • 10
  • 64
  • 118
0

But I should note that I've not executed .class files this way.

Bzhenko
  • 33
  • 1
  • 6
  • 1) jPortable Launcher is a Windows-only program. The original question didn't limit distribution your class files to Windows platforms 2) "I've simply took jre1.8.0_74 folder from that machine, where Java was installed and put it into my Eclipse folder - compiling & running well!" - I suspect you have JDK installed within Eclipse installation? – Alexander Petrovskiy Feb 27 '23 at 12:33