Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。

15
HTTP 415 JSON不支持的媒体类型错误
我正在使用JSON请求调用REST服务,并且它会返回HTTP 415 "Unsupported Media Type"错误。 请求内容类型设置为("Content-Type", "application/json; charset=utf8")。 如果我在请求中不包含JSON对象,则效果很好。我正在使用google-gson-2.2.4JSON库。 我尝试使用几个不同的库,但这没什么区别。 有人可以帮我解决这个问题吗? 这是我的代码: public static void main(String[] args) throws Exception { JsonObject requestJson = new JsonObject(); String url = "xxx"; //method call for generating json requestJson = generateJSON(); URL myurl = new URL(url); HttpURLConnection con = (HttpURLConnection)myurl.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "application/json; …

30
Hibernate错误:org.hibernate.NonUniqueObjectException:具有相同标识符值的另一个对象已与会话关联
我有两个用户对象,而在尝试使用以下方法保存对象时 session.save(userObj); 我收到以下错误: Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.pojo.rtrequests.User#com.pojo.rtrequests.User@d079b40b] 我正在使用创建会话 BaseHibernateDAO dao = new BaseHibernateDAO(); rtsession = dao.getSession(userData.getRegion(), BaseHibernateDAO.RTREQUESTS_DATABASE_NAME); rttrans = rtsession.beginTransaction(); rttrans.begin(); rtsession.save(userObj1); rtsession.save(userObj2); rtsession.flush(); rttrans.commit(); rtsession.close(); // in finally block 我还尝试session.clear()过保存之前的操作,但仍然没有运气。 这是第一次,当用户请求到来时,我得到了会话对象,所以我得到了为什么要说该对象存在于会话中的信息。 有什么建议?
114 java  hibernate  orm 


9
遍历Scala中的Java集合
我正在编写一些使用Apache POI API的Scala代码。我想遍历java.util.IteratorSheet类中包含的行。我想在for each样式循环中使用迭代器,因此我一直试图将其转换为本地Scala集合,但不会走运。 我已经看过Scala包装器类/特征,但是看不到如何正确使用它们。如何在不使用while(hasNext()) getNext()循环的冗长样式的情况下遍历Scala中的Java集合? 这是我根据正确答案编写的代码: class IteratorWrapper[A](iter:java.util.Iterator[A]) { def foreach(f: A => Unit): Unit = { while(iter.hasNext){ f(iter.next) } } } object SpreadsheetParser extends Application { implicit def iteratorToWrapper[T](iter:java.util.Iterator[T]):IteratorWrapper[T] = new IteratorWrapper[T](iter) override def main(args:Array[String]):Unit = { val ios = new FileInputStream("assets/data.xls") val workbook = new HSSFWorkbook(ios) var …

27
如何确定二叉树是否平衡?
那些学期已经有一段时间了。在一家医院担任IT专家的工作。现在尝试着做一些实际的编程。我现在正在研究二叉树,我想知道确定树是否高度平衡的最佳方法是什么。 我在想一些这样的事情: public boolean isBalanced(Node root){ if(root==null){ return true; //tree is empty } else{ int lh = root.left.height(); int rh = root.right.height(); if(lh - rh > 1 || rh - lh > 1){ return false; } } return true; } 这是一个好的实现吗?还是我错过了什么?

13
如何从Java项目中的相对路径读取文件?java.io.File找不到指定的路径
我有一个包含2个软件包的项目: tkorg.idrs.core.searchengines tkorg.idrs.core.searchengines 在包(2)中,我有一个文本文件ListStopWords.txt;在包(1)中,我有一个类FileLoadder。这是代码FileLoader: File file = new File("properties\\files\\ListStopWords.txt"); 但是有这个错误: The system cannot find the path specified 您能给出解决方案吗?谢谢。
113 java  file 

6
为什么一个40亿迭代的Java循环仅花费2毫秒?
我在装有2.7 GHz Intel Core i7的笔记本电脑上运行以下Java代码。我打算让它测量完成2 ^ 32次迭代的循环所需的时间,我预计大约需要1.48秒(4 / 2.7 = 1.48)。 但是实际上只需要2毫秒,而不是1.48 s。我想知道这是否是底层任何JVM优化的结果? public static void main(String[] args) { long start = System.nanoTime(); for (int i = Integer.MIN_VALUE; i < Integer.MAX_VALUE; i++){ } long finish = System.nanoTime(); long d = (finish - start) / 1000000; System.out.println("Used " + d); }
113 java  for-loop  jvm 

5
如何在Java中正常处理SIGKILL信号
当程序收到终止信号时,如何处理清理? 例如,我连接到一个应用程序,该应用程序希望任何第三方应用程序(我的应用程序)finish在注销时发送命令。finish当我的应用被a破坏时,发送该命令的最好的说法是kill -9什么? 编辑1:不能杀死-9。谢谢你们纠正我。 编辑2:我想这种情况将是当那只叫杀死与ctrl-c相同
113 java  sigkill 

5
从Java中的字符串在内存中创建File对象
我有一个接受File作为参数的函数。我不想创建/写入新文件(我没有对文件系统的写访问权)以将字符串数据传递给该函数。我应该补充一点,字符串数据在文件中不存在(所以我无法从文件中读取数据)。 我可以使用流并将其“广播”到文件对象吗?
113 java  file  file-io 

6
Java枚举方法-返回相反方向的枚举
我想声明一个枚举Direction,它具有一个返回相反方向的方法(以下语法不正确,即,不能实例化枚举,但它说明了我的观点)。这在Java中可行吗? 这是代码: public enum Direction { NORTH(1), SOUTH(-1), EAST(-2), WEST(2); Direction(int code){ this.code=code; } protected int code; public int getCode() { return this.code; } static Direction getOppositeDirection(Direction d){ return new Direction(d.getCode() * -1); } }
113 java  enums  enumeration 


14
轻巧的替代Hibernate吗?[关闭]
从目前的情况来看,这个问题不适合我们的问答形式。我们希望答案会得到事实,参考或专业知识的支持,但是这个问题可能会引起辩论,争论,民意测验或进一步的讨论。如果您认为此问题可以解决并且可以重新提出,请访问帮助中心以获取指导。 7年前关闭。 我有一个用户Java程序,希望将数据存储在轻量级数据库(例如Derby或Sqlite)中。我想在程序中使用数据抽象层。Hibernate似乎需要大量配置,并且对于我所需要的东西来说过于矫kill过正。什么是Hibernate的轻巧替代品?

4
Java三元运算符与<JDK8兼容性中的if / else
最近,我正在阅读Spring Framework的源代码。我不明白的地方在这里: public Member getMember() { // NOTE: no ternary expression to retain JDK &lt;8 compatibility even when using // the JDK 8 compiler (potentially selecting java.lang.reflect.Executable // as common type, with that new base class not available on older JDKs) if (this.method != null) { return this.method; } else …

7
Java是否有类似C#的ref和out关键字的东西?
类似于以下内容: 参考示例: void changeString(ref String str) { str = "def"; } void main() { String abc = "abc"; changeString(ref abc); System.out.println(abc); //prints "def" } 出示例: void changeString(out String str) { str = "def"; } void main() { String abc; changeString(out abc); System.out.println(abc); //prints "def" }
113 c#  java 


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.