How to locate a java class in a .sh file on Ubuntu? - java

I want to run a .sh file named ubiqstart.sh. using java. Specifically, I just want to execute a java class when running this .sh file. But I keep getting this error: Error: Could not find or load main class com.ubiq.update.MainEntry. Seems like the system is not able to locate where the java class is at. However, when I directly type in and execute this command in terminal (under this exact directory), it runs perfectly.
The script I use in the .sh file is: "../java/bin/java" -cp "./*" com.ubiq.update.MainEntry.
I wonder what causes this conflict and what should I do to make my .sh file to do the same thing as directly running the script.

When you are using older editors like vim or nano, just simply add a space after your classpath, like: com.ubiq.update.MainEntry . Especially if you convert the script from windows' .bat to Linux .sh

Related

Apache Fop manual start : "java -jar" is ignoring absolute path

I am experiencing a lot of trouble while trying to run a jar file in the terminal. Until now, my command line looks like this:
java -jar /usr/src/Apache_FOP/fop-2.3/fop/build/fop.jar
Notice that i have already builded Apache FOP from source using Apache Ant, and the fop.jar is running fine when i run this line in the same folder. The problem is, whenever i'm trying to run it from any different directory, (let's call it TestFolder), i get this error:
Unable to start FOP:
java.lang.RuntimeException: fop.jar not found in directory: /home/evandro.teixeira/TestFolder (or below)
at org.apache.fop.cli.Main.getJARList(Main.java:71)
at org.apache.fop.cli.Main.startFOPWithDynamicClasspath(Main.java:130)
at org.apache.fop.cli.Main.main(Main.java:219)
I'm not really sure of how is this related to Java, ApacheAnt or ApacheFop, but for some reason the fop.jar file is indeed executed if ai run the command line like this:
cd /usr/src/Apache_FOP/fop-2.3/fop/build ; java -jar fop.jar
The problem with this attempt is that i can't pass any parameters to fop.jar that are not within the same directory when using $# in a shell script, and i really need them to be outside the fop.jar folder.
Any ideas?

Java command line using an imported jar

I am sure this is a stupid question and it must have been asked by every java programmer before. But I cannot find a related question at all.
This talks about subdirectories but I don't have any subdirectories as they are all in the same directory as the java file and the directory I executed the command line from Executable jar file error
This solution gives me the same error as I am writing below: Java command line with external .jar
Others (I don't have links to) talk about Eclipse and other IDE but I am not using an IDE, just a Linux terminal.
I am trying to import a public jar file from http://www.hummeling.com/IF97. The downloaded jar file has been renamed to if97.jar.
I have a java file called steam.java with these commands inside the file:
'
import com.hummeling.if97.IF97;
IF97 H2O = new IF97(IF97.UnitSystem.ENGINEERING);
System.out.println("test H2O table PSpecificEnthalpy(1): "+H2O.specificEnthalpyPT(1,300));
System.out.println("test H2O table PSpecificEnthalpy(5): "+H2O.specificEnthalpyPT(5,300));
'
But I do not know how to run this file in the command line.
I successfully compiled by typing:
'javac -cp if97.jar ~/test/steam.java'
Now I have a file called steam.class
But when I execute it with:
'java steam -cp if97.jar'
or
'java steam -jar if97.jar'
I get error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hummeling/if97/IF97
at steam.start(steam.java:364)
at steam.main(steam.java:341)
Caused by: java.lang.ClassNotFoundException: com.hummeling.if97.IF97
I am trying to execute this in Linux Ubuntu 16.04 using Terminal. Both the files (steam.java and if97.jar) are in the same Home directory where I execute the javac & java command on.
I believe (or I'm mistaken) that the problem is that java isn't able to find the jar file. But I don't know why.
Please advise, thank you in advance.
You need to specify the class name after the JVM options, because whatever coming after the class name are considered arguments for the class, not the JVM.
Try this:
'java -cp if97.jar steam'

.jar file is not working after being published from Eclipse [duplicate]

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.

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.

Why can I not execute my java project from a script?

I am working on a project that has many module jar files, one of which conains my main class; I am trying to write a shell script that will setup the class path and then start the application; here is my script.
#!/bin/sh
java -cp "modules/*;lib/*" com.example.Launcher
In this example, com.example.Launcher is the class that contains public static void main(String[] args)...
The issue that I am facing is that when executing my script by ./myscript I am give the output
Error: Could not find or load main class com.example.Launcher
This would be simple, there is something wrong with the classpath right?
But if directly from the command line I execute
java -cp "modules/*;lib/*" com.example.Launcher
the exact same command from the script, in the directory the script resides, everything works just fine.
Any thoughts?
side note
I am running this via CygWin
another side note
This might be an issue with sh in CygWin. I coppied this build to a CentOS machine and tried executing it, changing the ; to a : allowed for execution from the script.
The issue persists on my window machine even with the change.
a third sidenote
It would again appear that the issue is with sh in CygWin, my final solution was this:
launcher.sh will invoke java using a : in the classpath
launcher.bat will invoke java using a ; in the classpath
On Unix-like systems the seperator is a : (not a ;). Change
java -cp "modules/*;lib/*" com.example.Launcher
to
java -cp "modules/*:lib/*" com.example.Launcher

Categories

Resources