开发问题汇总

来源:互联网 发布:平面设计和淘宝美工 编辑:程序博客网 时间:2024/06/18 08:52

1.解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

1.添加M2_HOME的环境变量

2.Preference->Java->Installed JREs->Edit 选择一个jdk,

  添加  -Dmaven.multiModuleProjectDirectory=$M2_HOME

2.实例代码

public class String {

public static void main(String args[]) {

CreateJsonString();

}

public static String CreateJsonString() {

System.out.print("请做一个有心人");

return null;

}

}

运行结果:

错误: 在类 String 中找不到 main 方法, 请将 main 方法定义为:

   public static void main(String[] args)

否则 JavaFX 应用程序类必须扩展javafx.application.Application

2.RabbitMQ在windows下的安装

MQMessage Queue)产品有很多,为了统一规范,消息协议也有很多,现在只需要知道JMS是其中一种协议,AMQP是另一种协议。RabbitMQ产品实现了AMQP协议。

RabbitMQ需要安装在服务器上,然后往你的代码里加入一个客户端jar包。

windows服务端下载

在本地调试之前,要先启动server端。

因为RabbitMQ是用Erlang开发,不是Java,所以你要像装JVM一样,先装个Erlang运行环境。

Erlang 19.2 windows安装包

  安装完成后开始菜单会有 RabbitMQ Service - start,启动后可以在浏览器打开控制台http://127.0.0.1:15672

  RabbitMQ 它依赖于Erlang,需要先安装Erlang。首先确定你的window电脑是32位还是64位,以下的安装以window 64位电脑举例。

  运行行Erlang/OTPotp_win64_17.5.exe)的安装步骤,按下一步

  进行RabbitMQ Server的下载与安装,

  直接运行rabbitmq-server-3.5.2.exe,选择要安装的目录,进行安装

  检查RabbitMQ是否运行正常,

  我的这里的安装是安装在D:\java\RabbitMQ_Server

  打开命令行命令行,进入RabbitMQ的安装目录: cd rabbitmq_server-3.5.2\sbin

  ,输入 rabbitmqctl status ,如果出现以下的图,说明安装是成功的,并且说明现在RabbitMQ Server已经启动了,运行正常。


3.net.sf.json.JSON 使用

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.xml.XMLSerializer;
public class CreateJson {
public static void main(String[] args) {
// createJsonString();
// createJson();
createJson1();
}
public static String createJsonString() {
// json对象
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "雨轩");
jsonObject.put("age", "28");
jsonObject.put("sex", "男");
System.out.println(jsonObject.toString() + "\n");

Person person = new Person();
person.setName(jsonObject.getString("name"));
person.setAge(jsonObject.getString("age"));
person.setSex(jsonObject.getString("sex"));
System.out.println(person.toString());

JSONObject personJson = new JSONObject();
JSONObject jsObject = new JSONObject();
jsObject.put("name", "福尔摩斯");
jsObject.put("age", "29");
jsObject.put("sex", "男");
personJson.put("personJson", jsObject);
System.out.println(personJson.toString());

Person person1 = new Person();
JSONObject personJson1 = new JSONObject();
JSONObject jb = personJson1.fromObject(personJson.toString());
JSONObject jb1 = jb.getJSONObject("personJson");
person1.setName(jb1.getString("name"));
person1.setAge(jb1.getString("age"));
person1.setSex(jb1.getString("sex"));
System.out.println(person1.toString());


JSONArray personJsons = new JSONArray();
JSONObject jsObject1 = new JSONObject();
jsObject1.put("name", "福尔摩斯1");
jsObject1.put("age", "29");
jsObject1.put("sex", "男");
JSONObject jsObject2 = new JSONObject();
jsObject2.put("name", "福尔摩斯2");
jsObject2.put("age", "29");
jsObject2.put("sex", "男");
personJsons.add(jsObject1);
personJsons.add(jsObject2);
System.out.println(personJsons.toString());
return null;
}
// 创建jsonArray,JSONObject
private static void createJson() {
JSONObject personJson = new JSONObject();
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "雨轩");
jsonObject.put("age", "34轩");
jsonObject.put("sex", "男");
jsonObject.element("hight", "175cm");
personJson.element("personJson", jsonObject);
System.out.println(jsonObject.toString());
System.out.println(personJson.toString());
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "雨轩");
jsonArray.add(1, "福尔摩斯");
jsonArray.add(2, "杨蛋蛋");
jsonArray.element("小金猪");
jsonObject.element("jsonArray", jsonArray);
System.out.println(jsonObject.toString());
System.out.println(jsonArray.toString());
}
private static void createJson1() {
// JavaBean与json字符串互转
Person person = new Person("福尔摩斯", "35", "男");
JSONObject personJson = JSONObject.fromObject(person);
System.out.print(personJson.toString());

// json字符串转JavaBean
String jsondata = "{\"name\":\"福尔摩斯\",\"age\":\"58\",\"sex\":\"男\"}";
JSONObject jsonObject1 = JSONObject.fromObject(jsondata);
Person stu2 = (Person) JSONObject.toBean(jsonObject1, Person.class);
System.out.println(stu2);
// List转json字符串
List perList = new ArrayList();
perList.add(new Person("雨轩1", "34", "男"));
perList.add(new Person("雨轩2", "35", "男"));
perList.add(new Person("雨轩3", "36", "男"));
JSONArray jsonArray = JSONArray.fromObject(perList);
System.out.println(jsonArray.toString());
// json字符串转List
List list1 = new ArrayList();
String jsondatas = "[{\"name\":\"福尔摩斯\",\"age\":\"58\",\"sex\":\"男\"},{\"name\":\"福尔摩斯\",\"age\":\"58\",\"sex\":\"男\"},{\"name\":\"福尔摩斯\",\"age\":\"58\",\"sex\":\"男\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondatas);
for (int i = 0; i < jsonArray1.size(); i++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
Person per2 = (Person) JSONObject.toBean(jsonObject2, Person.class);
list1.add(per2);
}
System.out.println(list1.toString());
// Map转json字符串
Map map = new HashMap();
map.put("1", new Person("雨轩1", "12", "男"));
map.put("2", new Person("雨轩2", "13", "男"));
map.put("3", new Person("雨轩3", "14", "男"));
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);
// json字符串转Map
String jsonMapdata = "{\"1\":{\"age\":\"12\",\"name\":\"雨轩1\",\"sex\":\"男\"},\"2\":{\"age\":\"13\",\"name\":\"雨轩2\",\"sex\":\"男\"},\"3\":{\"age\":\"14\",\"name\":\"雨轩3\",\"sex\":\"男\"}}";
Map map1 = (Map) JSONObject.fromObject(jsonMapdata);
Set set = map1.keySet();
Iterator ite = set.iterator();
while (ite.hasNext()) {
String key = (String) ite.next();
JSONObject jsonObject = JSONObject.fromObject(map1.get(key));
Person stu = (Person) JSONObject.toBean(jsonObject, Person.class);
System.out.println(key + " " + stu);
}
// List转型JSONArray
List list = new ArrayList();
list.add(new Person("雨轩1", "34", "男"));
list.add(new Person("雨轩2", "35", "男"));
list.add(new Person("雨轩3", "36", "男"));
JSONArray jsonPersonArray = JSONArray.fromObject(list);
System.out.println(jsonPersonArray.toString());
// JSONArray转型List
List list2 = JSONArray.toList(jsonPersonArray, new Person(),
new JsonConfig());
Iterator ite1 = list2.iterator();
while (ite1.hasNext()) {
Person stu = (Person) ite1.next();
System.out.println(stu);
}
// Java数组转JSONArray
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonBoolArray = JSONArray.fromObject(boolArray);
System.out.println(jsonBoolArray.toString());
// JSONArray转Java数组
Object obj[] = jsonBoolArray.toArray();
for (Object o : obj) {
System.out.print(o + " ");
}
// XML转JSON
String xml = "<root>" + "<name type='type'>zhaipuhong</name>"
+ "<gender>male</gender>" + "<birthday>" + "<year>1970</year>"
+ "<month>12</month>" + "<day>17</day>" + "</birthday>"
+ "</root>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);
System.out.println(json.toString(2));
// JSON转XML
String jsonXmldata = "{\"root\":{" + "\"name\":\"zhaipuhong\","
+ "\"gender\":\"male\"," + "\"birthday\":{"
+ "\"year\":\"1970\"," + "\"month\":\"12\"," + "\"day\":\"17\""
+ "}" + "}" + "}";
JSONObject jsonObject = JSONObject.fromObject(jsonXmldata);
String xmlstr = new XMLSerializer().write(jsonObject);
System.out.println(xmlstr);
}
}

FastJson使用详解

阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson;
功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE 5.0以上版本;支持Android;开源 (Apache 2.0)
Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。


public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
*public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
*public static final  T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean 
*public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
*public static final  List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 
*public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。


package com.seleniun.ylq;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class FastJson {
public static void main(String args[]) {
CreateJson();
}
public static void CreateJson() {
List<Person> persons = new ArrayList<Person>();
Person person = new Person("雨轩", "20", "男");
persons.add(new Person("雨轩1", "23", "男"));
persons.add(new Person("雨轩2", "24", "男"));
persons.add(new Person("雨轩3", "25", "男"));
// 将集合或者对象序例化成JSON
System.out.println(JSON.toJSON(person));
System.out.println(JSON.toJSON(persons));
// 将集合或者对象序例化成JSONString
System.out.println(JSON.toJSONString(person));
System.out.println(JSON.toJSONString(persons));


// Json串反序列化成对象
Person personJson = JSON.parseObject("{\"name\":\"雨轩4\",\"age\":\"34\",\"sex\":\"男\"}", Person.class);
System.out.println(personJson.toString());
// 数组对象反序列化成集合
String jsonList = "[{\"name\":\"雨轩4\",\"age\":\"35\",\"sex\":\"男\"},{\"name\":\"雨轩5\",\"age\":\"36\",\"sex\":\"男\"}]";
List<Person> personList = JSON.parseArray(jsonList, Person.class);
System.out.println(personList.toString());


// 没有对象直接解析JSON对象
JSONObject personJsonObject = JSON.parseObject("{\"name\":\"雨轩4\",\"age\":\"34\",\"sex\":\"男\"}");
System.out.println(personJsonObject.toString());
System.out.println(personJsonObject.toJSONString());


// 没有对象直接解析JSON数组
JSONArray jarr = JSON.parseArray(
"[{\"name\":\"雨轩4\",\"age\":\"35\",\"sex\":\"男\"},{\"name\":\"雨轩5\",\"age\":\"36\",\"sex\":\"男\"}]");
for (int i = 0; i < jarr.size(); i++) {
JSONObject jsObject = jarr.getJSONObject(i);
System.out.println(jsObject.toJSONString());
}
}
}

4.Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoader
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4660)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
问题背景:
工程为maven工程,ContextLoaderListener类位于spring-web-3.1.0.RELEASE.jar包中。检查了maven的pom.xml,依赖引入正常。在工程Maven Dependencies视图中也能看到spring-web-3.1.0.RELEASE.jar包被正常引入进来了。
错误原因:
进入到tomcat的部署路径C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\springtest\WEB-INF下检查了一下,发现工程部署后在WEB-INF文件夹下没有生成lib目录,正常情况下,会生成lib目录并把工程的所有依赖jar包都部署到该目录下。
解决方案:
1.右键点击项目--选择Properties
选择Deployment Assembly,在右边点击Add按钮,在弹出的窗口中选择Java Build Path Entries
2.点击Next,选择Maven Dependencies
3.点击Finish,然后可以看到已经把Maven Dependencies添加到Web应用结构中了
操作完后,重新部署工程,不再报错了。然后我们再到C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\springtest\WEB-INF\目录下,发现工程WEB-INF目录下自动生成了lib目录,并且所有的依赖jar包也都已经部署进来。问题因此解决。


5.配置spring,配置
<context:component-scan base-package="包名" />时,报错:
The prefix "context" for element "context:component-scan" is not bound.
是因为该配置文件的<beans></beans>标签的属性少了东西。<beans>加上相应属性即可。如下图(红线上那一列即为需要加上的东西):
下面是需要添加的部分的文字版:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd


0 0