Jump to content

How to compile and run from command line

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
So at the moment I'm the guy who likes to do things from the command line.
I like compiling C/C++ from the command line but Java, I don't know how.
Right now I have this very simple mysql-test-program which I cannot run
from command line but from Eclipse, no problem. The only thing separating
this project from a very simple one is that I have added one Library to Java
Build Path (like in the picture I have attached).

How is this done in terminal ?

edit: I added the zip-package which contains the eclipse project files..

#2
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
If you have Eclipse, Ctrl+F11.
From the command line: javac mainClass.java && java mainClass
// d-_-b+

#3
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

ksemeks said:

If you have Eclipse, Ctrl+F11.
From the command line: javac mainClass.java && java mainClass
Thanks for the reply.
I had no problem with eclipse, works fine there.
And the command
javac Dat.java && java Dat
produces the following exception
com.mysql.jdbc.Driver
Like before, no change.

#4
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Well, where did you put that lib?
You should put it in your $JAVA_HOME/jre/lib/ folder.

Eclipse has automatically put the lib(mysql.jar) when calling the main class, while you have to do it manually.
// d-_-b+

#5
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
No go. Created the environment variable JAVA_HOME and pointed it to /usr/lib/jvm/java-6-sun/ and moved the library there.
This was done merely out of curiosity since I think that this solution is way too complicated and I'm thinking there has to be a way to just give the right options in the commands.

#6
chirag.jain18

chirag.jain18

    Newbie

  • Members
  • PipPip
  • 10 posts
Suppose you are in C: drive. Your .java source file class is in C:\myclass directory and mysql.jar is in C:\lib directory and your java is installed in c:\java\jdk1.6 directory. Lets go now:
from cmd prompt:

C:\> set classpath=C:\java\jdk1.6\lib;c:\lib\mysql.jar;
C:\> cd myclass
C:\myclass> javac Classname.java
C:\myclass> java Classname

#7
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

chirag.jain18 said:

Suppose you are in C: drive. Your .java source file class is in C:\myclass directory and mysql.jar is in C:\lib directory and your java is installed in c:\java\jdk1.6 directory. Lets go now:
from cmd prompt:

C:\> set classpath=C:\java\jdk1.6\lib;c:\lib\mysql.jar;
C:\> cd myclass
C:\myclass> javac Classname.java
C:\myclass> java Classname

I'm not working with Windows but I tried to convert your paths to linux:
CLASSPATH=/usr/share/java/:/usr/lib/jvm/java-6-sun-1.6.0.20/lib/
javac Dat.java
java Dat
com.mysql.jdbc.Driver
So,
no go..

Edited by denarced, 13 May 2010 - 07:12 AM.
classpath command was the wrong one


#8
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Ok, so chirag.jain18 was almost correct in his advice,
the classpath was the key and this I knew from the
beginning. I just didn't know how to execute it just
right. Here's how I finally got it right:
javac -classpath .:/usr/share/java/mysql.jar Dat.java 
java -classpath .:/usr/share/java/mysql.jar Dat
So this would've been easier with the CLASSPATH environment
variable as long as one remembers to put include both the library
and the current directory. That was the mystery I was missing.

You live, you learn.

Thanks for all the help. :thumbup1: