ArrayIndexOutofBoundsException when running jar file - java

I have a jar file that I would like to run. I originally was running it in a batch script but now I want to run it as a shell script. (I did not write this code so I do not have access to the main class)
My jar file is called MyFile.jar and I am using
java -jar MyFile.jar
However, I get an error saying:
ArrayIndexOutofBoundsException: Index 0 out of bounds for length 0
This is the only error I get.
In the batch script, the file is executed using
java -cp MyFile.jar com.mycomapnny.example.Main "path of where to put output"
I think it has something to do with needing arguments. But I am not sure what arguments to pass in since I can't look at the main class.
Any help would be appreciated. I can try to provide any additional information I can.

Related

Get rid of the script file that starts a jar

I have 3 files :
MyApp.jar
start.cmd
start.sh
Here is what's in the start files :
java -cp MyApp.jar -Xms256m -Xmx1024m com.companyname.launch.Launcher someArgs -DsomeParameter=true
As you can guess, I have to start the application by executing the correct start file, depending on the OS I'm using.
Is there a simple solution to get rid of the start files, and have only a system independent executable jar file ?
You can run this from Java.
Have a FirstMain which doesn't depend on anything. e.g. can be run without command line arguments. This main start another java program for the same JAR starting the RealMain with all the command line arguments you need.

How to pass parameters from command line to a java program through makefile

My first question in stack overflow... Kind of excited but still struggling in the problem.
Alright, My question is how to pass parameters from command line to a java program through makefile.
Honestly I don't really know wether my description is correct....... Cause I don't really know much about makefile... In my assignment, the description is that we must develop a Makefile for GNU make to build our program. For example, the command lines
make
mipsim -v < test1.cmd > test1.log
will build the ISS (a simulator we made) and then run it with debugging output, taking input commands from the file test1.cmd and writing result to test1.log.
I have finished the program but I don't know how to make the things above happen.
What I know so far is just to use makefile to make the .class file from .java file....
I have no idea about how to get test1.cmd as my input file's name and test1.log as my output
file's name from command lines.... I guess these two names probably will get into my program through String[] args in the main function...
Could anybody give me some help please?
Thanks
There is some confusion as to the issues.
First, compile Java using make is a little... iffy. (Most people use ant or maven.) However, if you don't mind a little overhead, you can do it using make. You probably should run make from a directory at the root of the Java package hierarchy. You can determine all Java files below using make macros. Hint: shell:
JAVA_FILES = $(shell find -name \*.java)
Then you run javac. (Make sure to define all path names to compilers etc. using make macros.) With Java, it's not easy to derive a make target, because .class files are not 1:1 w.r.t. java files. I just use a target "compile", depending on all the java files, and touch a file acting as a dummy target.
Second, the execution. To invoke a Java program that is not in an executable jar, you set the classpath (option -cp), specify the main class name and add command line parameters. I'd have to know what "mipsim" is - probably a shell script for doing just that. Anyway, a make target could be the log file:
%.log : %.cmd
${JAVA_HOME}/bin/java -cp ${ROOT} <$< >$#
Now, make test1.cmd should run your program.
Note: Redirection is not specified by program arguments; this is handled by the shell.
Quick comment on your question.
your makefile needs 2 targets. one for build and the other for the run.
all: build run
build:
(this is to build class file from your java source)
run:
put your java command line here like "java ..."
When you run "make", it will call "all" target. And all target will call "build" and "run" target, so just put one thing in one target and use the combinations.
Your java code.
Is your java takes input filename as argument or from stdin?
If you want to take input filename, then you can take it from args argument passed to your main(String[] args).
If you want to read from stdin then you can create a bufferedreader as below.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Hope this help.
(+1 please, if you like this answer)
There are a bunch of unrelated questions..
The syntax you are showing:
mipsim -v < test1.cmd > test1.log
would call an executable mipsim. Pass "-v" to the args[1]. Redirect test1.cmd to be the standrad input and test1.log be the standard output.
The output input redirection happens by the operating system so in c++ reading from std::cin will read from the file and writing to std::cout will write to test1.log
In java these will be redirected to System.in and System.out
About makefiles
Basically a make file rule looks like this:
<target>: <dependency1> .. < dependencyn>
~tab~ command
So just like it is possible to build a target that calls Javac. It is possible to build a target that calls Java.. and so you can build a test target and use it to execute any command you need
If you built a c++ executable then you can execute it from the makefile in the same way.
test: mipsim
mipsim -v < test1.cmd > test1.log
Your final question about pass parameter values to command line from make file do you mean something like this?
make PARA1=1 PARA2=ABC.c
You can use the parameters in your makefile..
test: mipsim
mipsim -v < $(INPUT_FILE) > $(OUTPUT_FILE)

Java - classNotFound Exception from Windows command line but not Eclipse

My code works fine in Eclipse, no build path errors or anything, however when I try to run my Java program from the command line, I get a classNotFound Exception on one of my inner classes. Not only do I not understand how I am getting this exception, but I am not even sure how to go about debugging it, since it looks and works fine in the IDE environment. Any help is appreciated!
Edit
I am writing a compiler for a subset of Java called J--, so I dont really want to get too into how it all works. But instead of calling javac HelloWorld.java I would call the equivalent j-- HelloWorld.java. javac works fine. You might say well the issue is with your code, but again it compiles and runs fine in Eclipse. So somewhere there seems to be a disconnect. Here is the Windows bash script if it helps:
set BASE_DIR=%~dp0
set j="%BASE_DIR%\..\"
set JAVA=java
set CPATH="%BASE_DIR%\..\lib\j--.jar;%BASE_DIR%\..\lib\spim.jar"
if "%CLASSPATH%" == "" goto runApp
set CPATH=%CPATH%;"%CLASSPATH%"
:runApp
%JAVA% -classpath %CPATH% jminusminus.Main "j--" %*
set JAVA=
set BASE_DIR=
set CPATH=
Edit
Thanks to Aubin, outputting the .jar file and comparing that with the class not found was how I was able to solve this conflict.
Usually Eclipse takes the sources from src and produces classes file into bin.
Try:
java -cp bin a.b.c.d.MyClass
To call your tool as j-- <args> you need to write a shell which embed the command:
java -cp bin a.b.c.d.MyClass $*

Batch File calling Java Issue

I currently have an open instance of the command prompt, it reads
java -cp .;jars/GUI.jar;jars/utils.jar;...(a bunch more jars in the same folder)... Test
where Test is my java program (class files already compiled).
When I hit enter, the program runs just fine. Now, I put the same exact line into a batch file for automatic runs but it gives me an error.
Batch File:
cd
java -cp .;jars/GUI.jar;jars/utils.jar;...(a bunch more jars in the same folder)... Test
pause
Error: Exception in Thread "Main" java.lang.NoClassDefFound Test (wrong name: Test)
I've been at this for hours today so at this point, this is the last thing I need to do, my brain is fried, am I missing something simple?
Or the Bash script doesn't know where the PATH to the Java installation. I'd check the environment variables as well.
Maybe you should try providing the full path of Test class: /x/y/Test
I'm not really sure of the exact rules but i guess the computer can't know which class you want to launch if Test.class is not in the current folder (current script folder? current cd/home folder?)

compile and execute java file batch file problem

Given this .bat file :
#ECHO OFF
echo === Compiling and executing bat file ===
md C:\my_project\dir_prog\class_files
copy ProgAudioJ.java C:\my_project\dir_prog
javac -d C:\my_project\dir_prog\class_files ProgAudioJ.java
java -classpath C:\my_project dir_prog.class_files.ProgAudioJ
I'm wondering what's wrong with it.
It just doesn't work.
I mean it places the class files in the directory called class_files (so the compiling process is ok) but the program doesn't run...
Thanks for your help MAX
Do you have defined a main class within your project and do you have defined the
public static void main(String[] args) method for this class? Remember, that method gets
called when running stuff from shell...
EDIT: you might find this comprehensive overview usefull: Running a Java Program from Command Prompt # SkylightPublishing.com
HTH
I think the first problem is that you need to specify the class_files directory on the classpath.
The second problem is that you have to specify the fully qualified name of the class that you want to run. This depends on the package that you have defined in the Java source file.
So something like this should work: (I'm assuming your class is in the default package, i.e. no package)
java -classpath c:\my_project\dir_prog\class_files ProgAudioJ
Can you provide the contents of the source file?
If your code and command is correct, I would suspect a delay, related to the asynchronous execution of the javac or to the filesystem.
Try adding a pause command in order to check if problem is the rapidity of execution of the commands or if the problem is in the command.

Categories