run java jar file on HP-UX as UNIX as "os.name" - java

I need to execute a jar file on HP-UX that I am not supposed to modify.
I unpacked it using jd-gui and found out that I am failing cause in java there is a condition to check the os, leading to different directions for win, macos, freebds, openbds, gnu and so on.
I am quite sure everything would work if I would be able to make my unix command line reply freebds or openbds to the java call
System.getProperty("os.name")
once executed from a jar file like:
java -jar myjar.jar
is there a way to achieve this? some kind of compatibility mode or a way to preset that parameter.

You can use the -D switch to specify system properties. In my experiment this (unexpectedly) even worked with pre-defined ones like os.name. Therefore this should work:
java -Dos.name=linux -jar myjar.jar

Related

Can't run .jar classes from .sh on Linux that work on Windows via .bat

I'm having a strange issue trying to run classes from an executable .jar file on Linux that none of the existing question threads I've sorted through seem to be able to resolve. I'll preface this in that I've never had to use Linux before and am only using it in this situation out of necessity, so it's possible I have overlooked something simple that I just didn't know could be causing the problem.
I can launch the classes from my .jar file without any issues on Windows via a .bat file with the following settings:
start "MyServer1" java -classpath ./*;Server.jar infoServer/StartInfoServer
start "MyServer2" java -classpath ./*;Server.jar loginServer/StartLoginServer
start "MyServer3" java -classpath ./*;Server.jar chatServer/start
start "MyServer4" java -classpath ./*;Server.jar gameServer/start
However, when I move to trying to launch these classes from the .jar on Linux, I get a "could not find or load main class" error. My .sh file is set up like this, and is placed in the same directory as my .jar file:
echo Starting Servers
java -cp Server.jar infoServer.StartInfoServer
java -cp Server.jar loginServer.StartLoginServer
java -cp Server.jar chatServer.start
java -cp Server.jar gameServer.start
echo All Done Starting Server
I've used ls from the Terminal to verify the .jar and .sh were being recognized as existing where they should be. (For future note, I'm using the Terminal from inside the directory containing my files.) I've made sure to make use of chmod to be sure both the .jar and the .sh have read/write/execute permissions and used ls -l to verify those permissions were indeed present. I've tried various forms of explicitly defining the classpath, such as using "/home/machine/Desktop/Folder/MyJar.jar", using pwd from the Terminal to ensure I'm getting the filepath correct. I've checked over my Java compatibility. (1.7.0_65 on Linux, 1.8.0_45 on Windows, with the .jar being created in Eclipse using 1.7 Compliance settings.) I can use unzip MyJar.jar from the Terminal and it will properly extract all my class files, allowing me to verify that my .jar isn't corrupted on my Linux machine and that the paths to the classes I'm trying to run are still the same on Linux as they are on Windows.
I do apologize if this is just a problem of inexperience overlooking something, but I can't think of or find any indication of what the problem could possibly be.
EDIT:
I've updated the question with some screenshots related to the problem:
https://gyazo.com/0ae2a2701aae734db21ef7c29200283b - General File Setup.
https://gyazo.com/d735d9cee57b4a92078c4b624d012b8c - Running the Shell via Terminal.
Other notes: jar -tf Server.jar works from the Terminal but not from inside the Shell script, which leads me to believe this may be some kind of visibility or pathing error, but I can't find any reason why that would be the case.

Making a "macro" command to run a program

I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!

creating 100% standalone executable jar that doesn't require the java command

so apparently if you create an executable jar, in order to run it you still need the java command:
java -jar something.jar
but what if I just want it to run without the java command, so just directly from the command line
something.jar
is there a way to export my java app in eclipse in order to accomplish such
On Unix systems you can append the jar file at the end of an executable script.
On Windows you have to create a batch file.
For instance in Unix:
$cat HelloWorld.java
public class HelloWorld {
public static void main( String ... args ) {
System.out.println("Hola mundo!");
}
}
$cat M.mf
Main-Class: HelloWorld
$cat hello
#!/bin/sh
exec java -jar $0 "$#"
$javac HelloWorld.java
$jar -cmf M.mf hello.jar HelloWorld.class
$cat hello.jar >> hello
$chmod +x hello
$./hello
Hola mundo!
In windows you have to create a batch file like:
::hello.cmd
javaw -jar hello.jar
Which has the same effect.
On Windows and OSX you can double click on the jar to run it, I'm pretty sure you may add a trigger on Linux too.
I hope this help
Excelsior JET - http://www.excelsior-usa.com/jet.html - claims to compile to native code and bring its own runtime support, so it does not require an existing JVM. Commercial product.
I have not tried it myself, but they have spent quite a bit of effort over the years to market JET as a great deployment method for precompiled binaries.
Also note that if you have an executable/runnable jar which works fine with "java -jar someting.jar" and you just want to be able to invoke it in a more convenient way, this is the job of the program accepting your command and launching the java command.
For Linux you can frequently add an alias saying that "something" expands to "java -jar something.jar", and some command interpreters allow for saying that all commands ending with jars should be executed specially. The exact details depend on which shell (command line interpreter) you are using.
What you need is a tool called 'Java Executable Wrapper'.You can use it to Pack all your class files to a Single Executable Package.
The One i recomment is launch4j,you can download it from sourceforge launch4j.sourceforge.net
Launch4J can be used to create standalone Executables (.exe) from a jar file for windows Environment.
The thing is, that Java gets interpreted by the JVM, so you'll at least need to ship it with your app.
To be a little more specific about this, Java gets kind of compiled to byte-code so it can be interpreted faster. But the Byte-Code can't run without the JVM. This is the nice side of Java: You don't need to recompile your Apps to run on other platforms like Linux or OS X, the JVM takes care of that (as it is written in native code and is recompiled for those platforms).
There are some compilers out there which can convert your Java code to something native like C which can then be executed without the JVM. But this isn't the idea behind Java and most of those tools suck at what they do.
If you want your App to run without any interpreter, you'll need to use a compiled language like C or C++
Java program runs on a JVM, for the first question I don't think there's a compiler that can do the job well. For the second question since a jar file is not an executable per se, there must be some sort of settings in the target machine, "executing" a jar file without providing the java command is a matter of convenience for the user. On Windows every file extension has a program associated with it, so .doc documents have (usually) Word as the program associated -that setting is set by the office installer, the java runtime also sets the setting for .jar files when you install it, but behind the scenes, java command will be used by the system. So the short answer to the second question is: depends on the target machine.

Bat file for jar

I'm using a bat file to run my jar. The code in my bat file is this :
#echo off
java -cp analyser.jar be.model.Start
pause
This works fine for windows.
But it doesn't do anything at linux. I also need to be sure it will run on Mac
Bat files are specific to Windows. You would need to execute the command in Linux and Mac in a manner that is specific to those platforms. The actual java call should work the same, I believe. The one change to the java line would be if you had multiple items in the classpath. In that case, you would need to use a colon as a separator instead of a semicolon (which is what Windows uses). (Thanks to khachik for that tip)
For Linux, you would use Shell programming using a BASH script. Here is a link that will describe what you need to do:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
For Mac, you would probably use an AppleScript. Here is an article on how to get started with AppleScripts:
http://www.macosxautomation.com/applescript/firsttutorial/index.html
For Linux, why not use a .sh (shell) file?
As Biggs~ alreay said, the actual Java call should remain the same.
Update:
You will also have to make it executable by changing it's user permissions. To do this, use: chmod +x thescript.sh

Jar file of java

I have created a java application and packed it into a jar file on a Windows platform. Now I wants to deploy that jar file on Debian Linux.
Will the same jar file work for Debian Linux?
Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
i.e.
java -jvm "MyJar.jar"
Will the above command work for Debian Linux?
Generally, it should. However this depends on a few conditions:
If you use native code (JNI) you must make sure that the native library is available for the target platform
You must make sure you have no paths hardcoded which are Windows specific (in fact you should even watch out for special characters like the Path seperator : vs. ;)
You cannot use Runtime specific code
Yes.
Jar files are portable across platforms, and the syntax of the jar command is the same on both Linux and Windows.
EDIT: You should use the latest version of Sun Java unless there is a very good reason not to. Installation instructions: http://wiki.debian.org/Java/Sun
1. Will the same jar file work for Debian Linux?
Yes. Hence the nature of Java (portable code)
2. Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
java -jar "MyJar.jar"
yes, the main idea of java is that it (should) run on different operating systems, as long as a java runtime is installed.
though i have never heard of the -jvm flag.
if you want to start a jar file you should use the -jar flag.
java -jar "MyJar.jar"
you can also read up on the Write once run anywhere principle.
I do my development on a mac but run in linux and windows environments without any problem. Key is not to use JNI, As everyone else have mentioned I would use java -jar "MyJar.jar"
Almost. Use:
java -jar "MyJar.jar"
And of course you shouldn't have used anything such JNI or runtime stuff
Yes, although you might want to do, in Linux:
java -jar YourJar.jar
Instead of:
java -jvm YourJar.jar

Categories

Resources