java


ERROR: Cannot find symbol — How do I get my class to compile when it depends on a class in a subfolder?


Inside my package alignmentparser I have a class named Subject where I have a string I want to convert to a JSON object. After researching how I might achieve this, I found the package JSON-java, which I downloaded and placed into my package as a subdirectory named JSON.
My classes' package statements state package alignmentparser;. I updated the package statements in each file in the JSON subdirectory to package alignmentparser.JSON;.
Now I can't get my TestRunner class (which depends on Subject) to compile. I get these error messages:
./alignmentparser/Subject.java:85: error: cannot find symbol
JSONObject json = new JSONObject(sb.toString());
^ symbol: class JSONObject location: class Subject
./alignmentparser/Subject.java:85: error: cannot find symbol
JSONObject json = new JSONObject(sb.toString());
^ symbol: class JSONObject location: class Subject
What can I do to get past these error messages?
EDIT: Here is the whole code, just in case there are errors I don't know about.
Subject.java
package alignmentparser;
import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;
public class Subject {
private String id;
private String sequence;
// Position on subject where alignment with query begins, inclusive
private int start;
// Position on subject where alignment with query ends, inclusive
private int end;
public Subject(String accessNum, int hitFrom, int hitTo, String seq) {
id = accessNum;
sequence = seq;
start = hitFrom;
end = hitTo;
getEntireSubject();
}
// Getters
public String getSequence() {
return sequence;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
// Fetches accession number from NCBI
// https://www.ncbi.nlm.nih.gov/books/NBK25498/#chapter3.Application_2_Converting_access
private void getEntireSubject() {
try {
// e post to Entrez
String link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/epost.fcgi?db=nucleotide&id=" + id;
URLConnection connection = new URL(link).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String webEnv = "";
String queryKey = "";
// Put together string to be scanned to find patterns
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
String result = sb.toString();
// Specify patterns to search for
String queryKeyRegEx = "<QueryKey>(\\d+)<\\/QueryKey>";
String webEnvRegEx = "<WebEnv>(\\S+)<\\/WebEnv>";
// Create Pattern objects
Pattern queryKeyPattern = Pattern.compile(queryKeyRegEx);
Pattern webEnvPattern = Pattern.compile(webEnvRegEx);
// Create Matcher objects
Matcher queryKeyMatcher = queryKeyPattern.matcher(result);
if (queryKeyMatcher.find()) {
queryKey = queryKeyMatcher.group(1);
System.out.println(queryKey);
}
Matcher webEnvMatcher = webEnvPattern.matcher(result);
if (webEnvMatcher.find()) {
webEnv = webEnvMatcher.group(1);
System.out.println(webEnv);
}
// e fetch to Entrez
link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&query_key="+queryKey+"&WebEnv="+webEnv+"&usehistory=y";
connection = new URL(link).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
r = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
sb = new StringBuilder();
while ((line = r.readLine()) != null) {
sb.append(line);
}
JSONObject json = new JSONObject(sb.toString());
System.out.println(json);
} catch (Exception e) {
System.out.println(e);
}
}
}
TestRunner.java
package alignmentparser;
import java.util.*;
class TestRunner {
private String query = "ATCCTGTCTGTGTTAGGAG-AGTCTACTTC-TTAACNGAGGGATTCANTNTTTCCTGCANAGGCGGCCGTCNATGAANACCCTGTTTGTGGACAGCTACNGNGAGATGCTTTTNTTTCTGCAGTCACTGTTCATGCTGGCCACCGTGGTGCTGTACTTCANCCACCTCAAGGAGTATGTGGCTTCCATGGTATTCTCCCTGGCCTTGGGCTGGNCCAACATGCTCTACTACNCCCGCGGTTTCCAGCAGANGGGCATCTATGCCGTCATGATANANAANATGATCCTGAGAGACCTGTGCCGTTTCATGTTTGTCTACGTCGTCTTNTTGTTCGGNTTTTCCACAGCGGNGGTGACGCTGATTGAAGACGGNAANAATGACTCCCTGCCGTCTGAGTCCACGTCGCACAGGNGGNGGGGGCCTGCCTGCANGCCCCCCGATAGCTCCTACAACAGCCTGTACTCCACCTGCCTGGAGCTGTTCAAGTTCACCATCGGCATGGGCGACCTGGANTTCACTGANAACTATGACTTCAAGGCTGTCTTCATCATCCTGCTGCTGGCCTATGTAATTCTCACCTACATCCTCNTGCTCAACATGCTNATCGCCCTCNTGGGTGAGACTGTCAACAAGATCGCACAGNNAGAGCAAGAACATCTGGAANCTGCAGAGAGCCATCACCATCCTGNACACGGAGAAGAGCTTCCTTAAGTGCATGAGGAAGGCCTTCCGCTCAGGCAAGCTGCNTGCANGTGGGGTACACACCTGATGGCAAGGACGACTACCGG";
private String midline = "||||||||||||||||||| |||||||||| || | |||||||||| | ||||||||| ||||||||||| ||||| ||||||||||||||||||||| | ||||||||||| |||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||| |||||||||||||||||| |||||||||||||||||||||| | || ||||||||||||||||||||||||||||||||||||||| ||||||| |||||||| ||||||||||||| ||||||||||||||||||||| || |||||||||||||||||||||||||||||||||||| || ||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||| ||||||||| ||||||||||||||||||||||||||||| ||||||||||||||||||| |||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||| ||||||||||||||||||||||||||||||||||||";
private Subject subj = new Subject("NM_080704", 1710, 2495, "ATCCTGTCTGTGTTAGGAGGAGTCTACTTCTTTTTCCGAGGGATTCAGTATTTCCTGCAGAGGCGGCCGTCGATGAAGACCCTGTTTGTGGACAGCTACAGTGAGATGCTTTTCTTTCTGCAGTCACTGTTCATGCTGGCCACCGTGGTGCTGTACTTCAGCCACCTCAAGGAGTATGTGGCTTCCATGGTATTCTCCCTGGCCTTGGGCTGGACCAACATGCTCTACTACACCCGCGGTTTCCAGCAGATGGGCATCTATGCCGTCATGATAGAGAAGATGATCCTGAGAGACCTGTGCCGTTTCATGTTTGTCTACATCGTCTTCTTGTTCGGGTTTTCCACAGCGGTGGTGACGCTGATTGAAGACGGGAAGAATGACTCCCTGCCGTCTGAGTCCACGTCGCACAGGTGGCGGGGGCCTGCCTGCAGGCCCCCCGATAGCTCCTACAACAGCCTGTACTCCACCTGCCTGGAGCTGTTCAAGTTCACCATCGGCATGGGCGACCTGGAGTTCACTGAGAACTATGACTTCAAGGCTGTCTTCATCATCCTGCTGCTGGCCTATGTAATTCTCACCTACATCCTCCTGCTCAACATGCTCATCGCCCTCATGGGTGAGACTGTCAACAAGATCGCACAGG-AGAGCAAGAACATCTGGAAGCTGCAGAGAGCCATCACCATCCTGGACACGGAGAAGAGCTTCCTTAAGTGCATGAGGAAGGCCTTCCGCTCAGGCAAGCTGC-TGCAGGTGGGGTACACACCTGATGGCAAGGACGACTACCGG");
public static void main(String[] args) {
TestRunner analyzer = new TestRunner();
analyzer.identifyMismatches();
analyzer.identifyResidue(547);
}
/**
* Identifies the residue encoded by the codon at the specified codon count.
*/
private void identifyResidue(int pos) {
HashMap<String, ArrayList<String>> codons = assembleCodonMap();
// Set base position of first codon in the CDS (to be retrieved from GenBank later)
int posOfCDS = 276;
// Adjust given codon count to account for any difference between CDS / query length
// NOTE: Subtract 3 to account for conversion between codon count and base count
int adjustedPos = (pos * 3) - (subj.getStart() - posOfCDS) - 3;
System.out.println(adjustedPos + subj.getStart());
if (adjustedPos < 0) {
System.out.println("Codon " + pos + " is not viewable in this alignment.");
//System.out.println("You can view codons " + (posFirstCodon + 1) + " through " + ((2495 - 276) / 3));
}
// Get codon
String found = "";
for (int i = 0; i < query.length(); i += 3) {
if (i == adjustedPos) {
found = query.substring(i, i + 3);
System.out.println(found + " at " + (i + subj.getStart()));
}
}
// Identify residue that codon translates to
for (String codon : codons.keySet()) {
if (codons.get(codon).contains(found)) {
System.out.println("Residue is " + codon);
}
}
}
private HashMap<String, ArrayList<String>> assembleCodonMap() {
ArrayList<String> ala = new ArrayList<String>();
ala.add("GCT");
ala.add("GCC");
ala.add("GCA");
ala.add("GCG");
ArrayList<String> arg = new ArrayList<String>();
arg.add("CGT");
arg.add("CGC");
arg.add("CGA");
arg.add("CGG");
arg.add("AGA");
arg.add("AGG");
ArrayList<String> asn = new ArrayList<String>();
asn.add("AAT");
asn.add("AAC");
ArrayList<String> asp = new ArrayList<String>();
asp.add("GAT");
asp.add("GAC");
ArrayList<String> cys = new ArrayList<String>();
cys.add("TGT");
cys.add("TGC");
ArrayList<String> gln = new ArrayList<String>();
gln.add("CAA");
gln.add("CAG");
ArrayList<String> glu = new ArrayList<String>();
glu.add("GAA");
glu.add("GAG");
ArrayList<String> gly = new ArrayList<String>();
gly.add("GGT");
gly.add("GGC");
gly.add("GGA");
gly.add("GGG");
ArrayList<String> his = new ArrayList<String>();
his.add("CAT");
his.add("CAC");
ArrayList<String> ile = new ArrayList<String>();
ile.add("ATT");
ile.add("ATC");
ile.add("ATA");
ArrayList<String> leu = new ArrayList<String>();
leu.add("TTA");
leu.add("TTG");
leu.add("CTT");
leu.add("CTC");
leu.add("CTA");
leu.add("CTG");
ArrayList<String> lys = new ArrayList<String>();
lys.add("AAA");
lys.add("AAG");
ArrayList<String> met = new ArrayList<String>();
met.add("ATG");
ArrayList<String> phe = new ArrayList<String>();
phe.add("TTT");
phe.add("TTC");
ArrayList<String> pro = new ArrayList<String>();
pro.add("CCT");
pro.add("CCC");
pro.add("CCA");
pro.add("CCG");
ArrayList<String> ser = new ArrayList<String>();
ser.add("TCT");
ser.add("TCC");
ser.add("TCA");
ser.add("TCG");
ser.add("AGT");
ser.add("AGC");
ArrayList<String> thr = new ArrayList<String>();
thr.add("ACT");
thr.add("ACC");
thr.add("ACA");
thr.add("ACG");
ArrayList<String> trp = new ArrayList<String>();
trp.add("TGG");
ArrayList<String> tyr = new ArrayList<String>();
tyr.add("TAT");
tyr.add("TAC");
ArrayList<String> val = new ArrayList<String>();
val.add("GTT");
val.add("GTC");
val.add("GTA");
val.add("GTG");
HashMap<String, ArrayList<String>> codons = new HashMap<String, ArrayList<String>>();
codons.put("alanine", ala);
codons.put("arginine", arg);
codons.put("asparagine", asn);
codons.put("aspartic acid", asp);
codons.put("cysteine", cys);
codons.put("glutamine", gln);
codons.put("glutamic acid", glu);
codons.put("glycine", gly);
codons.put("histidine", his);
codons.put("isoleucine", ile);
codons.put("leucine", leu);
codons.put("lysine", lys);
codons.put("methionine", met);
codons.put("phenylalanine", phe);
codons.put("proline", pro);
codons.put("serine", ser);
codons.put("threonine", thr);
codons.put("tryptophan", trp);
codons.put("tyrosine", tyr);
codons.put("valine", val);
return codons;
}
/**
* Prints out mismatches in the alignment of subject and query.
* Base positions are referenced from subject.
*/
private void identifyMismatches() {
// Loops through each character in the midline
for (int i = 0; i < midline.length(); i++) {
// If character is a space, denoting a mismatch
if (midline.charAt(i) == ' ' && query.charAt(i) != 'N') {
// Print out the characters at the same position in subject & query
System.out.println(subj.getStart() + i);
System.out.println(subj.getSequence().charAt(i) + ">" + query.charAt(i));
}
}
}
}
EDIT 2: Screenshot of my folder structure.
Firstly, unless you are planning to modify the source code for JSON-java, you don't really need to include the project as source code. Just modify your project dependency to include the JSON-java jar artifact or include it in your classpath so that the necessary classes can be found by the run time.
Secondly, if you really want to include the project as source code and compile it yourself, you still don't need to change the package structure of the project. You can compile it and build the artifact (jar) outside your application.
Thirdly, if you still want to include the project as source code in your own package, you need to modify the package declaration of each and every class within this project. It looks like you have already tried it. But to make sure you have, for example changed the package name of the class JSONObject from org.json to alignmentparser.org.json? Also, when you want to use one or more of these JSON-java classes in your own code, you need to import those classes. E.g. import org.json.JSONObject. I see in your code sample (Subject.java), that you are not doing it. Once you have fixed the package renaming and included the necessary class import statements, you should not get any compiler error.

Related Links

How to avoid Checkstyle's FinalClass module to complain when there are inner classes inheriting from base?
Check if id exists for multiple realm models
Keylistener won’t work in Jframe after Jar export
how to find the second largest value in the tree [closed]
Using amazon SNS to call two nodes of the same application
How to read a String array members one by one using RxJava and call a network API for the first result?
Java Selenium - Waiting on slow websites
Java: why left click on a button is faster than right button
Blocking code is executed in the one thread
Is there a way I can generate source:jar after Proguard obfuscation?
Two update operations overriding each other even protected by locking
SSL connectivity through certificates
Issue with HQL query
Can't retrieve JSON from Youtube API v3
Generate list of all possible unfinished tic-tac-toe positions in Java, considering symmetry [duplicate]
java regex catching space or &

Categories

HOME
log4j
jdo
openmp
iterator
vmware
ckeditor
cross-browser
ezpublish
ios-charts
basic
fortumo
decimal
dynamics-crm-online
windows-7-x64
evopdf
oracle-coherence
oxyplot
swingx
one-hot-encoding
php-openssl
facet
bootstrap-duallistbox
tooltipster
applozic
gsoap
junit5
xquery-3.0
ioio
create-table
avcapturesession
gammu
bower-install
file-format
bitbucket-pipelines
espeak
streamsets
sharefile
lumberjack
sfdc
gulp-sourcemaps
eclipse-gef
smartcontracts
jquery-nestable
paxos
pnotify
worker-thread
kbuild
nodebb
typed-lambda-calculus
boost-preprocessor
capacity
iso8601
blacklist
objective-c-swift-bridge
grails-tomcat-plugin
autorest
angstrom-linux
phpcas
browser-link
r-tree
ora-00900
python-3.2
ruby-2.2
quicklisp
coveralls
sframe
deis
operation
iiviewdeckcontroller
p2
sorl-thumbnail
camanjs
zend-route
yorick
mechanize-ruby
undefined-reference
concurrent-collections
aqtime
ril
eventual-consistency
limejs
chuck
mt
psi
xmlspy
cinema-4d
recent-documents
floating
fluent-interface
dentrix
database-management
web-architecture
audio-capture
libs
procedural-music
paul-graham

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App