Run a java program from anywhere - java

I am running a java program using a .bat file. It works well after double clicking direcctly on the .bat, that's not the problem.
What I want now, is to run that .bt file (and the java program by extends) from the cmd at first, and then to be able to compile it from any other machine.
I have followed at first the following answer : How do I run a java program from a different directory? , but it didn't work for me.
Here is my .bat file :
#echo on
set CLASSPATH=%CLASSPATH%;.;lib/console.jar;lib/log4j-1.2.13.jar;lib/prog1.jar;lib/prog1_newOption.jar;lib/org.hamcrest.core_1.3.0.v201303031735.jar;lib/RXTXcomm.jar;lib/trace.jar;lib/xercesImpl.jar;lib/xml-apis.jar
java -cp "$/prog1_newOption/src/main/Main" %UsersCommand%
pause
exit
%cmd%
Maybe have I to look for the main path in the .bat file and then parse it the the "java command programm" line?
Thank you so much for your help

Related

Can't execute java program on Raspberry Pi on startup

I use a Raspberry Pi 3 Model B with Raspbian Jessie.
I have a java program that I want to compile and run on start-up. I figured I need to launch it through /etc/rc.local. I have a command to compile the java-files and one command to run them.
sudo javac -cp
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/.jar
/home/pi/Desktop/MultiSensor_v2.0_Java/.java
sudo java
-Djava.library.path="/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/lib"
-cp "/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/opencv-320.jar:/home/pi/Desktop/MultiSensor_v2.0_Java/"
Main &
When I run these commands from the terminal it works perfectly. I tried to put these commands in the /etc/rc.local file before exit 0. On reboot, the compilation works but the actual program never executes. How do I get my java program to run upon startup using the two commands above?
I had this problem, too. I solved it with the following workaround:
I created a shell-script that runs the java programm. Then I called the shell-script on startup.
I managed to solve it by doing a bash-script as #ILikeCOding said, but I had to change the location from were it was called.
I moved the bash-script to the root directory and edited the autostart script located in:
/home/pi/.config/lxsession/LXDE-pi/autostart
In that file, I added a line to launch my script. I think this works because the autostart-script launches when you log into your user and not on boot, therefore the program can launch properly. Not sure if that's the case, but it works so I'm happy.

Run Java Project From Linux Cmd

I've written a java program and I'm trying to run it from cmd both in Windows and Linux.
I first compiled and then run it , and it worked just fine on Windows.
The problem comes up when I'm trying to do the same thing on Linux. The commands I've used on Linux are :
javac -cp aspose-cells.jar:aspose-words.jar:aspose-slides.jar ConvertToPdf.java
java -cp aspose-cells.jar:aspose-words.jar:aspose-slides.jar ConvertToPdf
The first command , which starts with javac, works just fine and outputs the ConvertToPdf.class file. The problem comes up after running the second one , which outputs the following error:
Error: Could not find or load main class ConvertToPdf
I'd like to know what am I doing wrong , and I'd be grateful if anyone could point me to the solution.
Thanks in advance.
Add the current directory . to the classpath:
java -cp aspose-cells.jar:aspose-words.jar:aspose-slides.jar:. ConvertToPdf
your working directory is not included in class path.
do this :
export CLASSPATH=$CLASSPATH:<your-jars>:.
it should work !!!

Could not find main class when double clicking jar- tried quite a few solutions

I searched through other posts related to this problem but haven't been able to find a solution that fixes my problem.
I can use java -jar helloworld.jar via command prompt and it runs perfectly, however if I double click on it command prompt briefly opens and I glimpse the error in the title.
One person offered a solution in the form of going into the registry and checking HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\Shell\Open\Command value is set properly to javaw file pathway, which it as:
"C:\Program Files\Java\jdk1.8.0_05\bin\javaw.exe" -jar "%1" %*
Another solution offered reference to having in PATH Environment Variable, I have this in there:
C:\Program Files\Java\jdk1.8.0_05\bin
I am certain the MANIFEST file is correct, as the project runs fine in Netbeans and of course when you try Command Prompt java -jar helloworld.jar it works perfectly.
Can anyone advise? I have been fiddling with this for hours to no avail and I can't determine where I am going wrong. Thanks stackoverflow!
EDIT:
I have also tried:
assoc .jar
and
ftype jarfile
which return
.jar=jarfile
and
jarfile="C:\Program FIles\Java\jdk1.8.0_05\bin\javaw.exe" -jar "%1" %*
respectively.
here is the manifest information:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.2 Created-By:
1.8.0_05-b13 (Oracle Corporation) Class-Path: X-COMMENT: Main-Class will be added automatically by build Main-Class: mypackage.HelloWorld
If you are using Eclipse, while exporting Jar it asks for Main class. There you have to select a Main class from multiple classes.
From command line -
java -cp name.jar classname
here the classname should be a fully qualified class name.
My guess is that your JAR file works fine.
If you want to see the return values when executing the program, it is better to redirect the input and output to a small Swing Window. Or you would make the console wait for some input.
I ran into a problem a while ago (running IPConfig) where the command would run and the command prompt would close before I could see anything. It sounds like this is what's happening here, as far as the window going away before you can see the error.
Try opening command prompt first and running the file without the "java -jar" command and switch. As I understand things, double-clicking the .jar will try exactly that (running the file without the java command), error out, and close the command prompt window. Maybe trying this will keep the command prompt open and allow you to see the error message. It may also describe the problem (double-clicking doesn't use the "java -jar" command as far as I know).

Java -cp on linux

I made a program which runs fine on windows. When I moved it over to CentOS, I got this error:
Error: Could not find or load main class org.wbc.WBCController
This is the file setup and .sh on linux:
And this is the file setup and .bat on windows:
Does anybody know what the problem is, and how I can fix it?
Java will respond with this error even if it cannot find the file wbc.jar. I am guessing that that is your problem. You might want to see that your are executing the shell script from within the right working directory.
Check to see if you can run wbc.sh from the console or put this in wbc.sh to make sure it searches for the jar in the same directory as the shell script:
#!/bin/sh
java -cp `dirname $0`/wbc.jar org.wbc.WBCController

I Cant get a .jar file to run after being double clicked on windows 7

i have tried several things so far, i have used a .bat file to run it, and it opens up the command prompt, but closes right after opening. i have a manifest.txt file already created along with the .jar. If i type -java -jar DesktopApplicationRunner.jar in a command windows that is already up it will run the program.
my .bat currently says:
-java -jar DesktopApplicationRunner.jar
Apologies just read your comment saying you have inputs. your problem is using the bat file, unless you have user inputs, the application will start, open the command line, then close when finished, the best way is to manually type in the command line, so it doesn't close the view down afterwards, sorry if this makes no sense.
-Ian

Categories