I'm used to writing java in eclipse and using it to import JARs, set up workspace, etc. however, when I want to deploy a project to my server and call it from a bash script like java Main arg1 arg2 something isn't write I get:
java.lang.ClassNotFoundException
is my classpath set wrong? what is eclipse doing behind the scenes? when I do
echo $CLASSPATH
I always get a blank line. The below (executed from bin folder) doesn't work either:
java -cp "~/Code/Java/SQL/MySQLAccess/bin;/usr/bin/java;/Users/Me/Code/Java/SQL/MySQLTest/mysql-connector-java-5.1.18-bin.jar" Main arg1 arg2 ... arg7
this gives me a java.lang.ClassNotFoundException: Main error
your class path is not set correct, you can use command java -classpath path_for_java javafile argumnets
As far as eclipse is concerned you can go to project->properties->java build path->libraries and you will get to know from where eclipse is referring to java libraries.
If you plan to be moving this around and deploying to potentially different servers, look into exporting your project as a jar. You can include the sql jars inside your jar and your invocation from the script will look like
java -jar Main.jar
From Eclipse, you can go to File->Export, then pick 'jar' from the menu and follow the steps.
Your manifest will be something like
Main-Class: Main
Class-Path: mysql-connector-java-5.1.18-bin.jar
Related
I'm having big problems running my java api war file from the command line after ive packaged it with maven.
I'm trying to run it using the following command from the target folder where my war file is located.
java -cp silverkissen.war se/consys/silverkissen/heroku/Main
And alot of other variations but i just get
Error: Can't find or load main class se/consys/silverkissen/heroku/Main
My war file lies in path ..\Silverkissen-API\target\silverkissen.war
My heroku main class lies in path ..\Silverkissen-API\target\classes\se\consys\silverkissen\heroku\Main.class
Thankful for any help.
The main issue is most likely because the class files isn't incorporated into the silverkissen.war file. Meaning there is no Main function in the war file itself. Or that the entry function is some where else.
Or that it's packaged in some mysterious way that is beyond my understanding that's specific to maven, heroku etc.
But assuming you're standing in the project root structure, one level before the target folder usually where you'd normally have src, target, pom.xml and system.properties. I'd try running the following:
java -cp target/classes:target/dependency/* se.consys.silverkissen.heroku.Main
And if you're on Windows that'd be:
java -cp target\classes;target\dependency\* se.consys.silverkissen.heroku.Main
That aught to do it. This will execute your project with class-path's in the runtime. Assuming my limited knowledge of Java is correct.
Someone with more experience can probably explain in detail why this would work.
Open command prompt in the location where your jar/war located
And then run below command
java -jar silverkissen.war
I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files
So, I have exported my project in both Netbeans and Eclipse and when I try to
java -jar myproject.jar
I get this prompt
In my project I have some libraries which are located inside of src in Netbeans and out of src in Eclipse as it should (please correct me if I'm wrong) The libraries are included via:
Java Build Path > Add JARs...
I've done some research and it seems that I have to change my JAVA CLASSPATH or somethng like that but I don't know exactly how to do it.
The project works perfectly when I compile it and run it, but it crashes after I build it into a Jar file.
By the way, if it isn't clear enough I'm on Ubuntu 14.04
You need to create the path for the jar files and pass it on the command line.
Something like this:
ftp_jar=${Utils_home}/bin/ftpClientUtil.jar
net_jar=${Utils_home}/bin/commons-net-3.3.jar
jsch_jar=${Utils_home}/bin/jsch-0.1.51.jar
java -cp .:$jsch_jar:$net_jar:$ftp_jar com.myplace.ftputils.SFTPClientUtil $*
Run your program as:
java -cp .:[path-of-lib1.jar]:[path-of-lib2.jar] -jar myproject.jar
replace [path-of-libX.jar] with actual path of your libraries.
I want to create our own jar which has some simple methods like a print method.
I created the jar of that java project and imported it into an other project I am building path also.
The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class.
i am using eclipse 3.4 (Eclipse Ganymede (3.4))version
Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.
To compile:
javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java
To Run:
java -cp .:PathToMyJar/MyJar.jar MyMainClass
The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.
If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.
I would like to use an external library (e.g. Google's Guava) for my Java program. I use Eclipse, so I downloaded Guava's jar (and source) and followed Adding a Java library to the project classpath to add it to Eclipse and to the buildpath of my project.
This works fine: I can run the program from Eclipse and from the runnable jar I export from Eclipse, but I get an error when I try to run directly from the bin/ dir, as I used to do before:
Exception in thread "main"
java.lang.NoClassDefFoundError: com/google/common/base/Joiner
What should I do?
If you're running the class file directly from the project bin directory then you may have to specify the classpath manually:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses MyClassHere
Youll have to tell Java where to find the library:
java -cp <path-to-lib-jar>;myJar.jar my.package.MyMainClass
or if you wanna use a jar file you can set the library path in the MANIFEST
check here for an explanation.
Have you tried java -cp guava.jar ... ?
To run the program on console as precise as possible with when you run it from Eclipse, you need to run it from the root directory of the project (not from bin) and don't forget to mention the classpath (http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/classpath.html)
So for example on root you will run:
java -classpath lib/guava.jar;bin packageName.className