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.
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.
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
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.
Related
How to run a java file that has external jars in Linux. Also how to run it in Windows?
I tried the following, but not working.
javac -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application.java
thanks
------EDITED------
Now my class file got generated after compiling without any error. But when i run the file its showing following error
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar C:/com/example/Application
Exception in thread "main" java.lang.NoClassDefFoundError: C:/com/example/Application
Caused by: java.lang.ClassNotFoundException: C:/com/example/Application
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Any suggestion...
If you are running on linux, then there should be different directory structure from windows. Also the classpath on linux should contains paths separated path by colon(:) instead of semi-colon(;)
Put your jars in a directory on linux machines, may be in /var/tmp/myjars. And then compile your code using the below command:
javac -cp /var/tmp/myjars/lib1.jar;/var/tmp/myjars/lib2.jar Application.java
EDIT
As you have updated your question. You need to run your class on windows as mentioned here. I am taking few assumptions here that your Application class is in com.example package. If that is the case then you can run it from the folder containing the com folder as mentioned here:
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar com.example.Application
javac is the Java compiler. after compiling the program the running on windows should work like this
java -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application
You can try this
java -cp classpath=%classpath%;< jar-file full-path>; ClassName
Ex: java -cp classpath=%classpath%;c:/lib/lib1.jar;c:/lib/lib2.jar com.ab.cd.MyClass
I am new to java programming and I am getting the much-maligned error "ClassNotFoundException" error.
The strange thing is is that it compiles fine:
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
But when I try to run it, I get the following error:
steve#steve-ThinkPad-T61:~/facebook$ java FacebookProg
Exception in thread "main" java.lang.NoClassDefFoundError:
org/scribe/builder/ServiceBuilder
at FacebookProg.main(FacebookProg.java:8)
Caused by: java.lang.ClassNotFoundException: org.scribe.builder.ServiceBuilder
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)
... 1 more
I checked online and it seems that java can't find the library at runtime that it was able to find at compile time. So tried the following variations:
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
java -cp /usr/share/java/ FacebookProg
export CLASSPATH="/usr/share/java"; java FacebookProf
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
I checked several places on StackOverflow and google and still can't figure out why. I'm new to java, so there's probably a simple solution, but I can't find it. I am using Sun Java 1.6 64-bit on Ubuntu 11.04. The scribe-1.3.0.jar file is in "/usr/share/java" which, I believe, is the canonical place to put java packages.
The barebones code is here (in case it matters):
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.oauth.*;
public class FacebookProg {
public static void main (String args[]) {
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey("blah_blah")
.apiSecret("blah_blah")
.build();
}
}
The classpath has to point to BOTH the directory of the external library you are using AND the class you are trying to run itself. Try this:
Windows:
java -cp .;/usr/share/java/scribe-1.3.0.jar FacebookProg
Linux:
java -cp .:/usr/share/java/scribe-1.3.0.jar FacebookProg
By the way , to compile it you should have run this:
javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
This should work fine if you compiled the FacebookProg.class in same directory. You can try java -cp /usr/share/java/scribe-1.3.0.jar:/locationOfFacebookProg.class directory/ FacebookProg
This
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
means you are running the FacebookProg class, not compiling it.
If you leave the -cp ... out, you are leaving the vital classpath out, so the JVM cannot find the classes FacebookProg requires.
To compile, you need
javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg.java
(note the javac, instead of java to invoke the compiler)
To run, you already know how to.
Also, you have errors in the follwoing lines:
export CLASSPATH="/usr/share/java"; java FacebookProf
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
The first misspells FacebookProg and does not have the required jar on the classpath, the second has the wrong path to the jar. Try
export CLASSPATH="/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
Also, make sure the jar is indeed located at /usr/share/java/scribe-1.3.0.jar
suppose that, the directory of your program is
$HOME
$HOME/lib/*.jar
you can write a script like:
for file in "$HOME/lib/*.jar"
do
if [ -f $file ]
then
CLASSPATH=$CLASSPATH:$file
else
echo ignore $file
fi
done
java -cp $CLASSPATH FacebookProg
I am having some issues running my compiled java code from the command line. I have written it and compiled it using the IntelliJ IDE (where everything runs fine if done within the IDE), but wish to now run it from the command line.
Compiling from the command like (using javac) also works fine, but running (with java) does not.
I am almost certain this is a classpath issue but cannot seem to fix it. From my searching prior to posting this I found a post telling me to run the "set PATH=\%PATH\%;"C:\Program Files\Java\jdk1.6.0_21\bin" command and then try running java. I have also tried various arguements I have found for -cp and -classpath. The error is :
Exception in thread "main" java.lang.NoClassDefFoundError: Share/class
Caused by: java.lang.ClassNotFoundException: Share.class
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Share.class. Program will exit.
You're doing:
java -cp ... Share.class
Do
java -cp ... Share
Or if it's in a package
java -cp ... path.to.Share
You should not be supplying the class file as an argument, you should be supplying the fully qualified class name.
If your class is in the current directory and uses the default (empty) package, it will just be
java -cp . Share
or
java Share
The classpath is not used to point to the java executable, it's used to point to the various directories/jar files which contain your class files (at the root of the package structure).
See also
java - the Java application launcher (manual for invoking java)
I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.
I have a java project that's been heaped on me, and I know little to nothing about java. Especially compiling from the windows command line.
I have two Jars that I need to compile a simple "hello world" program with.
Here's my "build.bat"
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
Then, I:
C:\jdk1.6.0_21\bin\java sample
This spits back the error:
Exception in thread "main" java.lang.NoClassDefFoundError: sample
Caused by:
java.lang.ClassNotFoundException:
sample
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
No matter how I set up my classpath, I cannot for the life of me get this HELLO WORLD program to run.
Can someone please help me out? I'm pulling my hair out.
You can also specify the classpath for the interpreter to locate your class:
java -classpath "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; Sample
This would run your class from any working directory.
Run your program from the directory with the class in it:
C:\Users\FREYERA\Desktop\Test\>java sample
CLASSPATH (normally) includes the current directory.
If you have "sample.class" in the current directory, and you also need classes in test1.jar and test2.jar, this should work:
java -cp "test1.jar;test2.jar;." sample
Afer running this command
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
It would have created a .class file at this location "C:\Users\FREYERA\Desktop\Test\". i.e. sample.class
You need to either go to this folder location and run your java command to execute the program. Make sure that your "JAVA_HOME" environment variable is set.
Or you can copy the sample.class file to "C:\jdk1.6.0_21\bin\" folder and run the command.
I'm working through a ANTLR (a language processing library) book and there are many examples that should be easy to compile using the command line.
Some information to get te problem:
antlr-3.2.jar contains the library classes. I added the antlr-3.2.jar to the CLASSPATH environment variable (Windows 7) and when compiling the classes with javac everything works fine.
This is what i execute to compile my program:
javac Test.java ExprLexer.java ExprParser.java
Test.java contains my main()-method whereas ExprLexer and ExprParser are generated by ANTLR. All three classes use classes contained in the antlr-3.2.jar. But so far so good. As I just said, compiling works fine.
It's when I try to execute the Test.class that I get trouble.
This is what I type:
java -cp ./ Test
When executing this, the interpreter tells me that he can't find the ANTLR-classes contained in the antlr-3.2.jar, altough I added an entry in the CLASSPATH variable.
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/runtime/Cha
rStream
Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Test. Program will exit.
I'm using Windows 7 and Java 1.6_20. Can someone tell what is going on? Why will the interpreter not look in the jar-Archive I specified in the CLASSPATH?
I found some kind of workaroud. I copied the antlr-3.2.jar into the directory where the Test.class is located and then executed:
java -cp ./;antlr-3.2.jar Test
This worked out. But I don't want to type the jar-Archive everytime I execute my test programs. Is there a possibility to tell the interpreter that he should automatically look into the archive?
I'm using Windows 7 and Java 1.6_20.
Can someone tell what is going on? Why
will the interpreter not look in the
jar-Archive I specified in the
CLASSPATH?
-cp on the commandline overrides the CLASSPATH variable. There is no convenient way to do what you're trying to do. I'd suggest creating an ant script, shell script, or shell alias if you don't want to type out the full classpath each time.
Alternatively, you could put your Test application into its own jar file with a manifest that tells it to include antlr-3.2.jar in the classpath.