JDBC not working once program compiled - java

I have just completed my first Java Program, it works inside netbeans...but when I clean and build and run the executable jar file, it opens the GUI but none of the database functions work. I tried using JSmooth but I was either doing something wrong or it was not working on my computer. Im assuming that when i run the program outside of netbeans it cannot find the JDBC jar? how can i fix this?

Yes, most probably you are getting a ClassNotFoundException in the background. If you are starting your application from command line, you can specify jars to be on the classpath as follows:
java -cp jdbcdriver.jar my.package.MainClass

Related

Classpath set but java.lang.NoClassDefFoundError com/google/protobuf/MessageOrBuilder error?

I am trying to run a java program that uses protobuf.jar, but I keep getting this error.
I have set my classpath variable in linux so that:
CLASSPATH=/home/.../src/PlaceServer.class:/home/.../src/protobuf.jar:/home/.../src
export CLASSPATH
But then when I run my program in the command line after reading in the jar.
java PServer
I get this:
java.lang.NoClassDefFoundError: com/google/protobuf/MessageOrBuilder
However when I run another program that also reads the same jar, this one called BServer
java BServer
It works fine, and correctly as I want it. I even tried running under these commands instead
java -cp .protobuf.jar PServer
And it still did not work for PServer.
However, if I run the same programs on my Macbook from the command line (also in Eclipse in either OS) I do not get this Exception and it all works fine.
Thanks for your help!
There are a couple of things to check to get rid of this error:
Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.
The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution
When you run the application in Eclipse, the IDE resolves this by using the .classpath file inside the project folder. When you build an application (create the jar), you could accidentally omit this class, or change its location.
What you need to do is to first open the jar, and make sure that the class in question is in fact inside the jar, in the same path. Then, go through the list above.

What are the differences "under the hood" between running from jruby and a warbled jar?

I am having a problem where a libgdx program runs fine when run from the command line with jruby <filename>.rb but when I warble it into a jar and run java -jar <filename>.jar it runs and then closes without displaying a window.
Since I'm no expert on java or jruby I'd like to know what goes on under the hood, so to speak, in an attempt to solve this problem I'm having.
Maybe you don't have the libgdx library inside the jar, try this, execute the jar with:
java -cp gdx.jar -jar <filename>.jar
Assuming that the libgdx.jar is in the same folder. It's simply a classpath problem probably

noClassdeffounderror with external jar when running code from a different location

I'm learning Java and was trying to test my code on another machine but am running to the above error. I've looked at youtube videos, read forums(and SO) but still can't get this to work.
I basically wrote some code on my mac using eclipse that referenced an external jar file. I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path". The code works fine on my laptop.
But when I try to run it from a linux command line, I get the above error. I am taking the code from eclipse and copying it to a file(first_try.java) and then run this commands:
CLASSPATH=./jedis-2.0.0.jar;export CLASSPATH
javac first_try.java
java -classpath . first_try
but then I get the error: Exception in thread "main" java.lang.NoClassDefFoundError: redis/clients/jedis/Jedis I downloaded the program via wget and checked permissions and tried different variations. There was a similar question I found(out of the many related ones) that had a similar context as mine but it worked for the user to just type java filename (which is not working for me)
I am learning a bit of java code but have never been successful at running external jars. I'm wondering what I'm doing wrong and what I can do to permanently fix it(ideally I want to write code locally and then copy it and test it on another machine like this)?
Or is there a better way to deploy code that depends on third party jars to other systems?
You are building your class path env var but then you don't use the value in your java command. You need $CLASSPATH instead of the dot after -classpath
Good luck learning java, I have enjoyed using it for over 10 years now ;)

Application compiles and builds on Netbeans but not as an independent jar file?

Is it possible that your application compiles and builds flawlessly in Netbeans but when you build it and run it outside of Netbeans you get heaps of Exceptions??! (keeping in mind that the all the libraries and dependencies are ported along with the actual jar file).
I run the followng command
ant -f run
and my program runs flawlessly as it's supposed to, however, where I run the following command to actually build a jar file,
ant -f jar
and when I run the jar file, my application runs but does not perform the tasks it does when it is launched using the first command.
Any ideas why this is happening??! I'm totally lost with this behaviour and can't find why!
If the exceptions are ClassNotFound, then it means that you're missing JARs in the CLASSPATH. NetBeans is sorting it out for you, but when you run outside of it you have to manage it yourself.
keeping in mind that the all the libraries and dependencies are ported
along with the actual jar file
I'm not sure what the word "ported" ends up looking like for you, and without any real information I'm guessing, but your app isn't packaged correctly.
How are you attempting to build your project outside of NetBeans?
Inside NetBeans, if you created the project from within NetBeans, I believe that most of the time NetBeans will create all of its (by default) Ant targets in a file called nbbuild.xml.
Thus, if you're attempting to compile your project outside NetBeans from the command-line (however it exists on your system), you would then need to use the -f nbbuild.xml argument to the ant program to use the NetBeans-generated Ant file. So your command-line might look like this:
ant -f nbbuild.xml compile
Substitute, of course, the target you desire to run for compile in the example above.

Creating build of a Java project

I have a stand alone project in my eclipse.There are few external jar as well.The programme is executing well and giving required result. Now i need it to run from command prompt.and i just want to run the class file generated by eclipse. how i will be able to run it.When i am tring to run it from commend prompt it is giving class not found exception.I also tried to make an executable jar,but that is also not running.do i need to create executable jar or i can simply run it from command prompt..tell me the way out..
thanks
koushik
A step by step guide at
I recommend you to go through jar, class path and other basic building blocks of java for eg check this about jar
try using this command java -cp . package.name.className

Categories

Resources