0

I am new to run java files through shell script, this may be very basic question for those who are experienced or few knowledge in shell script. I have java file called Main.java under

C:\project\Tranmissions\com.abc.files\src\main\java\com\abc\files
   +Main.java

I have shell script called run.sh:

#!/bin/bash

java -Xmx300m -classpath com.abc.files.Main -mainclass com.abc.files.payroll.f401k.xyz.AdpCwMain -driver org.hsqldb.jdbc.JDBCDriver

exit $?

This script I have placed under

C:\project\Tranmissions\com.abc.files.

Now, I have downloaded cygwin to run the script as

./run.sh 

When I run this, I am getting following basic java error:

java.lang.NoClassDefFoundError: com/abc/files/Main
Caused by: java.lang.ClassNotFoundException: com.abc.files.Main
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.abc.files.Main.  Program will exit.
Exception in thread "main"

I am using STS(Eclipse) with maven command install to run the java files. and able to run my class "Main" java program.

3
  • Why don't you print the generated java command and see whether it's pointing to the correct jars? Commented Apr 2, 2014 at 17:59
  • Are you using Windows? Then why do you need cygwin when you can a Java command line from windows cmd? Commented Apr 2, 2014 at 18:04
  • @Alexandros, I have some other tasks to do with that. I have to run mule esb with this shell script. Commented Apr 2, 2014 at 18:28

3 Answers 3

1

You shouldn't need to specify -mainclass, just give the class with the main. Also the classpath has the com.abc prefix as does your class. You probably want the classpath to be the current dir and then give your class. If your JDBC isn't in the classpath you'll also get an error. Try something like so:

java -Xmx300m -classpath . \
  com.abc.files.payroll.f401k.xyz.AdpCwMain \
  -driver org.hsqldb.jdbc.JDBCDriver

Run with #!/bin/bash -x to show what commands are actually executed.

Sign up to request clarification or add additional context in comments.

1 Comment

Good suggestion to run with #!/bin/bash -x
0

You call "java" command on a file called Main.java, you should compile the Main.java class first with "javac". "java" is then used when the program ends in .class, ie Main.class

1 Comment

I am using Eclipse, it compiles and keeps files under target folder with maven command install.
0

You are not compiling the code , before you execute the line.

javac line is missing in run.sh

Why dont you try j2sch, it make your life even simpler.

1 Comment

How is a Java implementation of SSH relevant?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.