Java project running on eclipse but giving error while using batch - java

I made a project for my uni. where I need to pass command line arguments. It is running perfectly fine on eclipse but when I run it using a batch file.
my batch file looks like
set path = "c:\Program Files\Java\jdk-14.0.2\bin";
javac FileHand.java
java FileHand DirectBuffer 1024 Sample.txt
pause

Do not set %path% at all. If you want to 'hardcode' the full path to java, then do so; write C:\program files\....\javac, or SET JAVA_LOC=... and then %JAVALOC%\javac. But, this is clearly is not needed; you messed up your SET PATH statement and yet javac is being invoked, so, you should probably just remove the entire 'set path' line.
The problem is classpath. There is a file named DirectBuffer.class. It is somewhere - you said that 'it works in eclipse', which means eclipse can find this file, because you told it where it is. You need to tell javac where it is. You do this as follows:
javac -cp LOC1;LOC2;LOC3 FileHandjava
java -cp .;LOC1;LOC2;LOC3 FileHand DirectBuffer 1024 Sample.txt
where LOC1 is a path. It can be a directory, or a jar file. Your question does not make this clear, but let's say DirectBuffer' is in the com.foo.pkgpackage (so, you haveimport com.foo.pkg.DirectBuffer;` in your source file), then:
either:
cd (whatever you put for LOC1)
cd com\foo\pkg
dir
should print, amongst other things, 'DirectBuffer.class', or, if LOC1 is a jar file:
jar tvf (the jar file listed in LOC1)
should print com/foo/pkg/DirectBuffer.class, amongst other things. You've already told eclipse this, so now find those places where you did that and tell javac about it.

Related

How to find file path to a bash file when the folder is renamed or moved to a new drive?

I've made an executable jar file for a terminal game that can be opened by typing java -jar name.jar in the Terminal.
Then I made a .sh file inside the same folder as the jar file to open it by double-clicking the .sh. I asked how to do this here, where people told me to use the following code in the .sh.
#! /bin/bash
DIR=$(dirname "$0")
java -jar "$DIR/game.jar"
This worked for a while, but when I renamed the folder, I realised if I move the folder to a pen drive the whole thing stops working and I get this in the Terminal.
Error: Unable to access jarfile /Volumes/Hard
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
So how to find the file path to the folder the .sh and the jar are in, regardless of where it is, what its name is and what drive it is on?
Also, I'm using MacOS Mojave 10.14.4 if that's of any importance.
The error looks like the path does contain spaces, like probably /Volumes/Hard Drive/Users/something. The solution is to quote the command substitution.
Tangentially, don't use upper case for your private variable names.
But of course, the variable isn't really necessary here, either.
#!/bin/sh
java -jar "$(dirname "$0")/game.jar"
Nothing in this script uses Bash syntax, so it's more portable (as well as often slightly faster) to use sh in the shebang. Perhaps see also Difference between sh and bash
You can store the full path of the working directory using the environement variable $PWD, like in this example (done in 5min, it is just to show you how it is works) :
#!/bin/bash
DIR=$PWD
gamePath='java -jar '$DIR'/game.jar'
echo $gamePath
Wherever I will execute this script, it will shows up the working directory even if I change the name of the parent. Let me show you :
You can see that $PWD environnment variable works great.
Now, I will change the directory name from TestFolder to TestFolderRenamed and execute the script again :
So, in your case, change your code as following :
#! /bin/bash
DIR=$PWD
java -jar "$DIR/game.jar"
It should works.

The .exe I created with Launch4j doesn't run

I was able to successfully create a .exe file from an executable jar. From Launch4j I can test the wrapper, and the output on the log is what I expect. However if I try to run the exe from the command line or from Windows Explorer, nothing happens. No error, no output to the console as expected. The program is also supposed to edit a text file which does happen when I run the jar using a batch file, but not when I run the exe. This is all on the same computer so I doubt it is a problem with the JRE. I have searched StackOverflow extensively but found nothing that helps with this situation. I did find this post with a similar problem: Launch4J executable not executing as expected but nobody actually answered the question. Thank you in advance for helping
I found the issue. Under Header, I had to switch the Header type from GUI to Console. After that I was able to run the exe.
You may be accepting something as a command line argument which may be throwing an error like in my case .
For my case, it was the tick on "Signle Instance" tab: Allow only a single instance of the application.
Although I had killed the process, for some unknown reason the exe had been recognized as alive. Thus, a reboot of the PC is recommended.
These are the things I had to do for it to work on Windows 10:
First, make sure the executable JAR you created actually executes. I could never got the JAR to execute by double-clicking. Instead, I created a .bat file where I added the java -jar command-line instruction to execute the jar, including the VM arguments. For example, to execute foo.jar, the .bat file should contain java -jar --module-path %FX_HOME% --add-modules javafx.controls,javafx.fxml foo.jar. Once you get the JAR to execute without errors (pay attention to the command prompt window), then your JAR is ready. Then, do the following from the Launch4J app:
Create FX_HOME environment variable. Make sure it points to the lib folder of the downloaded Java FX distribution is located.*
Set JAVA_HOME environment variable.*
Make sure JAVA_HOME is the first entry on the Path variable. Verify by opening a command prompt and typing where java and hitting the ENTER key.
Under Header, make sure "GUI" is selected.
Under JRE, set Bundled JRE paths to %JAVA_HOME%.
Under VM Option, enter --module-path %FX_HOME% --add-modules javafx.controls,javafx.fxml
*If any of the paths contain spaces, make sure they are in quotes when you create the environment variable. For example: C:\Program Files\Java 17\bin, should be in quotes. This is because the command-line parameters are space-delimited and "Program Files" contains a space, fooling the command-line interpreter to think Program is the end of one parameter and Files is the beginning of another. By putting in quotes, the interpreter now knows the space is part of that single parameter that represents the path.

How does java locate a JAR file?

The JAR file locates at C:\Workbench\jars\antlr-4.4-complete.jar
The environment variables are:
CLASSPATH=.;C:\Workbench\jars\*
PATH=C:\Workbench\jars;...
I am trying with the following commands:
java -jar "C:\Workbench\jars\antlr-4.4-complete.jar" <-- OK
java -jar antlr-4.4-complete.jar <-- FAIL!
java org.antlr.v4.Tool <-- OK
I am totally confused about the failed one. I am expecting the PATH variable will be looped through to locate the jar file. But it seems not. Why?
My guess is, the implementation of java -jar command line doesn't use the PATH variable for searching jar file. But still, why?
The PATH is for looking up the command to execute, in this case java will be looked up on the PATH.
You will need to supply either an absolute or relative path to java -jar because the terminal (bash/windows/zsh/etc...) will not expand arguments in this way. CLASSPATH is used by Java to look up further jars, but it expects a correct path to the initial jar as the first argument.

How can I run my java class from the project root directory

I am compiling and running my Java project from the command line rather than eclipse for the first time and have a slight inconvenience that I just can't seem to find a solution too.
My project has a standard structure:
Project Directory
/src
/self
/redway
/myAPP.java
/bin
/libs
So I compiled using:
javac -sourcepath src src/self/redway/myApp.java -d bin
and so far so good...
I can run the program by navigating to the /bin and then just typing
java self.redway.myApp
BUT and this is really annoying. How do I run it from my project root directory?
I tried just
java bin/self.redway.myApp
and some other obvious ideas to no avail. I know it is a minor thing but it is super irritating and I'm sure there is a simple answer which I should have spotted immediately but I just can't find it!
Thanks.
You can add the java -cp option to specify the class path. E.g.
java -cp bin self.redway.myApp
you can write a shell script like the following:
cd [your bin directory]
java self.redway.myApp
call this script for example runProgram.sh, place it in the directory where you want to be when you run your program, and run it using
sh runProgram.sh
that would also save you time if your program is going to use other jars and you have to add them to the class path, then all you have to do is change your java command in the shellscript to become
java -cp [path to the jar]:[path to another jar]:...:[path to your bin folder] self.redway.myApp

Run Jar related .Java from cmd

I am trying to do "javac Classname.java" from cmd prompt, and this Classname.java requires Jfreechart libraries/jars, and runs fine if compiled from Eclipse (because project package has jars imported).
But I want to run the file from cmd prompt and its not able to show me the output. It comes with errors like: ("package doesn't exist"), how to fix it? I need the class file and also run JNI commands to create header file.
You need to set the classpath.
You can do this in 2 ways. Either use the -classpath or -cp option:
javac -cp jar1.jar;path/to/jar2.jar Classname.java
Or, if you need it to persist, use the CLASSPATH environmental variable:
set CLASSPATH=path1;path2
javac Classname.java
If you have already managed to run your code in Eclipse, then Eclipse can help you.
In the "Debug" view, you should have something like this remaining after you have run your code:
If you right-click the bottom "terminated" text and select "Properties", you will get something like this:
You can copy the command line content and use that to run your app from the command line, or use it to set the classpath as the other answers have advised.
You just need to add the directory paths and/or .jar libraries to your "-classpath" command-line argument.
Depending on how many libraries you've got, you might well wind up with a .sh script (Linux) or .cmd file (windows) that looks something like this:
http://ubuntuforums.org/showthread.php?t=230258
java -cp jts.jar:jcommon-1.0.0.jar:jfreechart-1.0.0.jar:jhall.jar:other.jar:rss.jar -Xmx256M jclient.LoginFrame .
If you're on Windows, you'd use ";" as a separator (instead of *nix ":").
'Hope that helps!

Categories

Resources