Archlinux下配置eclipse+tomcat7+mysql+jdbc

本文详细介绍了如何从零开始配置Eclipse for JavaEE进行Web应用开发,包括安装Oracle JDK、Tomcat7和MySQL,配置Eclipse及Tomcat插件,以及测试数据库连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做项目用到java web相关的东西,需要配置开发环境。没有考虑Myeclipse,觉得它太臃肿了,而且还收费,所以准备自己配置eclipse for java ee.

一、安装

当然这之前已经换成了oracle的jdk了

[aetf@Aetf-Arch-Laptop ~]$ yaourt -S jdk

tomcat7和mysql 都直接在官方源里面,eclipse-jee在aur里

[aetf@Aetf-Arch-Laptop ~]$ yaourt -S eclipse-jee tomcat7 mysql

jdbc也在aur里面,默认的安装位置是/usr/share里面,我的jdk在/opt/java,并且不想添加新的classpath了,所以稍微改了一下PKGBUILD

install -D -m644 \
       $srcdir/${connector}/${connector}-bin.jar \
       $pkgdir/usr/share/java/mysql-jdbc/${connector-bin.jar
ln -s ${connector-bin.jar $pkgdir/usr/share/java/mysql-jdbc/mysql-connector-java-bin.jar

改成了

install -D -m644 \
       $srcdir/${connector}/${connector}-bin.jar \
       $pkgdir/opt/java/jre/lib/ext/${connector-bin.jar
ln -s ${connector-bin.jar $pkgdir/opt/java/jre/lib/ext/mysql-connector-java-bin.jar

 

二、配置

配置tomcat

参考的网上教程说需要设置一些环境变量,但是我直接在tomcat7安装目录下运行脚本就已经可以正确的检测到我的配置了

    [aetf@Aetf-Arch-Laptop ~]$ cd /usr/share/tomcat7/bin
    [aetf@Aetf-Arch-Laptop bin]$ ./version.sh
    Using CATALINA_BASE:   /usr/share/tomcat7
    Using CATALINA_HOME:   /usr/share/tomcat7
    Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
    Using JRE_HOME:        /opt/java
    Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
    Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
    Server version: Apache Tomcat/7.0.35
    Server built:   Jan 17 2013 08:34:21
    Server number:  7.0.35.0
    OS Name:        Linux
    OS Version:     3.7.3-1-ARCH
    Architecture:   amd64
    JVM Version:    1.7.0_11-b21
    JVM Vendor:     Oracle Corporation
    [aetf@Aetf-Arch-Laptop bin]$ ./startup.sh
    Using CATALINA_BASE:   /usr/share/tomcat7
    Using CATALINA_HOME:   /usr/share/tomcat7
    Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
    Using JRE_HOME:        /opt/java
    Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
    Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
    Server version: Apache Tomcat/7.0.35
    Server built:   Jan 17 2013 08:34:21
    Server number:  7.0.35.0
    OS Name:        Linux
    OS Version:     3.7.3-1-ARCH
    Architecture:   amd64
    JVM Version:    1.7.0_11-b21
    JVM Vendor:     Oracle Corporation
    [aetf@Aetf-Arch-Laptop bin]$

然后用浏览器打开http://localhost:8080,应该已经显示正常的tomcat的页面。然后到eclipse里面,Window->Preferences->Server->Runtime Environment里面添加新的项,路径设置好。

 

配置eclipse

//就在写的时候,发现其实插件不装也是可以正常使用的,于是如果觉得麻烦跳过这点就好了

然后是eclipse的tomcat插件,叫做Sysdeo Tomcat Launcher Plugin. 一开始被网上的教程误导了,以为tomcatLauncher不支持tomcat7, 并且似乎这个插件的market页面不更新了,于是去找什么mongel,结果家里网不好用,market只能看就是安装不上= =

折腾了一下午最后在tomcatLauncher官网发现原来已经更新支持7了,果断下载安装。

地址:http://www.eclipsetotale.com/tomcatPlugin.html

我安装的时候版本是3.3, 下载回来解压丢到eclipse文件夹/dropins/下面就行了。

    [aetf@Aetf-Arch-Laptop Downloads]$ unzip tomcatPluginV33.zip 
    [aetf@Aetf-Arch-Laptop Downloads]$ sudo mv com.sysdeo.eclipse.tomcat_3.3.0/ /usr/share/eclipse/dropins/

重新启动eclipse就可以看到相应的工具栏出现了。

 

配置mysql

我的系统上之前就已经安装过mysql了,可能是之前解决依赖关系的时候装上的。但之前没有管过它,也没有配置。

首先用systemd来启动mysqld服务。

[aetf@Aetf-Arch-Laptop ~]$ sudo systemctl start mysqld

然后是添加root账户密码和删除test数据库。根据Arch Wiki里的说法,可以通过运行一个脚本来根据提示完成

[aetf@Aetf-Arch-Laptop ~]$ mysql_secure_installation

也可以直接用没有密码的root账户登陆后手动添加密码、删除test数据库

[aetf@Aetf-Arch-Laptop ~]$ mysql -uroot -p

mysql默认的配置是只允许本机访问,要想通过jdbc通过tcp访问,需要修改配置文件

[aetf@Aetf-Arch-Laptop ~]$ sudo vim /etc/mysql/my.cnf

找到一行

skip-network

将其注释掉

#skip-network

然后重新启动服务

[aetf@Aetf-Arch-Laptop ~]$ sudo systemctl restart mysqld

此时应该已经可以看到mysqld在监听3306端口了,状态是open。这个nmap是另外安装的,在extra仓库中。

[aetf@Aetf-Arch-Laptop ~]$ nmap -p 3306 localhost

Starting Nmap 6.25 ( http://nmap.org ) at 2013-01-24 22:34 CST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000028s latency).
Other addresses for localhost (not scanned): 127.0.0.1
rDNS record for 127.0.0.1: localhost.localdomain
PORT     STATE SERVICE
3306/tcp open  mysql

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
[aetf@Aetf-Arch-Laptop ~]$

然后通过mysql命令行界面添加新账户和数据库

[aetf@Aetf-Arch-Laptop ~]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.29-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on test.* to 'newuser'@localhost identified by 'password';
Query OK, 0 rows affected (0.33 sec)

mysql> \q
Bye
[aetf@Aetf-Arch-Laptop ~]$ mysql -unewuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.29-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

 

三、测试

新建一个java文件测试一下。

import java.sql.*;
import java.util.Properties;
public class DBDemo
{
        private static final String dbClassName = "com.mysql.jdbc.Driver";
        private static final String CONNECTION = "jdbc:mysql://127.0.0.1/javaWebApp";
        public static void main(String[] args) throws
                ClassNotFoundException,SQLException
        {
                System.out.println(dbClassName);
                Class.forName(dbClassName);

                Properties p = new Properties();
                p.put("user", "javaWebApp");
                p.put("password", "javaWebApp");

                Connection c = DriverManager.getConnection(CONNECTION,p);
                System.out.println("It works~");
                c.close();
        }
}

然后编译运行

[aetf@Aetf-Arch-Laptop Java]$ javac DBDemo.java
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
[aetf@Aetf-Arch-Laptop Java]$ java DBDemo
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
com.mysql.jdbc.Driver
It works~
[aetf@Aetf-Arch-Laptop Java]$

成功了~

 

 

 

参考:

MySQL - Arch Wiki

JDBC and MySQL - Archwiki

转载于:https://www.cnblogs.com/tech-cabin/archive/2013/02/05/2892934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值