Big Money
Big Money

Reputation: 10068

Is there a way I can allow someone to run my Java program without them seeing the code?

So basically I made a terminal-dependent Java program and want to start handing it out, but I can't seem to find a way to do that without giving them my code. When making a .jar file it only works on Windows.

When trying to run on Linux (CentOS) I get

  Exception in thread "main" java.lang.UnsupportedClassVersionError:

and

   Could not find the main class: MyClass. Program will exit.

I'm using the command java -jar MyJar.jar to run them on both. I am using JavaMail in this program so perhaps that could be an issue. There is only one class and it has a main in it.

If anyone even has any alternatives to using a jar file to deliver the program where the user can't read the code that'd be great too. Thanks!

Upvotes: 4

Views: 720

Answers (4)

Bill Shannon
Bill Shannon

Reputation: 29971

In addition to the version issue others have described, and the fact that you don't need to distribute the .java files, you'll need to set up a Class-Path entry in the manifest file of your application jar file to reference the JavaMail jar file, and you'll have to deliver your application jar file and the JavaMail jar file to users. That can be tricky to get right. You might want to look at One-JAR to solve that problem.

Upvotes: 0

Daniel Kaplan
Daniel Kaplan

Reputation: 67430

To me it sounds like the problem is that you compiled the code on an incompatible version that you're running the code on. For example, you compiled it with JDK 1.7 but the CentOS OS has only the java 1.5 version installed. Here is some more detailed information on the matter.

As for the title of your question, no, you don't have to give them the java source for them to run it. So something else is going wrong. Most likely the answer I gave.

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

The JDK that you've actually used to compile and build your Jar is more recent than compared to the one installed on your client's Linux machine. That's when JVM throws an

Exception in thread "main" java.lang.UnsupportedClassVersionError:

Ask your client to report his Java version by running the following on console

java -version

Then if you're using Eclipse set the target runtime of your project accordingly. If you're compiling your Java source files on your own use

javac -target 1.4 source/MyClass.java 

Upvotes: 3

Related Questions