AKB
AKB

Reputation: 5938

running java file from shell script

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.

Upvotes: 0

Views: 8670

Answers (3)

Sireesh Yarlagadda
Sireesh Yarlagadda

Reputation: 13736

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.

Upvotes: 0

j.con
j.con

Reputation: 889

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

Upvotes: 0

Paul Rubel
Paul Rubel

Reputation: 27242

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.

Upvotes: 1

Related Questions